Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * fs/f2fs/file.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/stat.h>
11#include <linux/writeback.h>
12#include <linux/blkdev.h>
13#include <linux/falloc.h>
14#include <linux/types.h>
15#include <linux/compat.h>
16#include <linux/uaccess.h>
17#include <linux/mount.h>
18#include <linux/pagevec.h>
19#include <linux/uio.h>
20#include <linux/uuid.h>
21#include <linux/file.h>
22#include <linux/nls.h>
23#include <linux/sched/signal.h>
24#include <linux/fileattr.h>
25#include <linux/fadvise.h>
26#include <linux/iomap.h>
27
28#include "f2fs.h"
29#include "node.h"
30#include "segment.h"
31#include "xattr.h"
32#include "acl.h"
33#include "gc.h"
34#include "iostat.h"
35#include <trace/events/f2fs.h>
36#include <uapi/linux/f2fs.h>
37
38static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
39{
40 struct inode *inode = file_inode(vmf->vma->vm_file);
41 vm_flags_t flags = vmf->vma->vm_flags;
42 vm_fault_t ret;
43
44 ret = filemap_fault(vmf);
45 if (ret & VM_FAULT_LOCKED)
46 f2fs_update_iostat(F2FS_I_SB(inode), inode,
47 APP_MAPPED_READ_IO, F2FS_BLKSIZE);
48
49 trace_f2fs_filemap_fault(inode, vmf->pgoff, flags, ret);
50
51 return ret;
52}
53
54static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
55{
56 struct folio *folio = page_folio(vmf->page);
57 struct inode *inode = file_inode(vmf->vma->vm_file);
58 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
59 struct dnode_of_data dn;
60 bool need_alloc = !f2fs_is_pinned_file(inode);
61 int err = 0;
62 vm_fault_t ret;
63
64 if (unlikely(IS_IMMUTABLE(inode)))
65 return VM_FAULT_SIGBUS;
66
67 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
68 err = -EIO;
69 goto out;
70 }
71
72 if (unlikely(f2fs_cp_error(sbi))) {
73 err = -EIO;
74 goto out;
75 }
76
77 if (!f2fs_is_checkpoint_ready(sbi)) {
78 err = -ENOSPC;
79 goto out;
80 }
81
82 err = f2fs_convert_inline_inode(inode);
83 if (err)
84 goto out;
85
86#ifdef CONFIG_F2FS_FS_COMPRESSION
87 if (f2fs_compressed_file(inode)) {
88 int ret = f2fs_is_compressed_cluster(inode, folio->index);
89
90 if (ret < 0) {
91 err = ret;
92 goto out;
93 } else if (ret) {
94 need_alloc = false;
95 }
96 }
97#endif
98 /* should do out of any locked page */
99 if (need_alloc)
100 f2fs_balance_fs(sbi, true);
101
102 sb_start_pagefault(inode->i_sb);
103
104 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
105
106 file_update_time(vmf->vma->vm_file);
107 filemap_invalidate_lock_shared(inode->i_mapping);
108 folio_lock(folio);
109 if (unlikely(folio->mapping != inode->i_mapping ||
110 folio_pos(folio) > i_size_read(inode) ||
111 !folio_test_uptodate(folio))) {
112 folio_unlock(folio);
113 err = -EFAULT;
114 goto out_sem;
115 }
116
117 set_new_dnode(&dn, inode, NULL, NULL, 0);
118 if (need_alloc) {
119 /* block allocation */
120 err = f2fs_get_block_locked(&dn, folio->index);
121 } else {
122 err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
123 f2fs_put_dnode(&dn);
124 if (f2fs_is_pinned_file(inode) &&
125 !__is_valid_data_blkaddr(dn.data_blkaddr))
126 err = -EIO;
127 }
128
129 if (err) {
130 folio_unlock(folio);
131 goto out_sem;
132 }
133
134 f2fs_wait_on_page_writeback(folio_page(folio, 0), DATA, false, true);
135
136 /* wait for GCed page writeback via META_MAPPING */
137 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
138
139 /*
140 * check to see if the page is mapped already (no holes)
141 */
142 if (folio_test_mappedtodisk(folio))
143 goto out_sem;
144
145 /* page is wholly or partially inside EOF */
146 if (((loff_t)(folio->index + 1) << PAGE_SHIFT) >
147 i_size_read(inode)) {
148 loff_t offset;
149
150 offset = i_size_read(inode) & ~PAGE_MASK;
151 folio_zero_segment(folio, offset, folio_size(folio));
152 }
153 folio_mark_dirty(folio);
154
155 f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE);
156 f2fs_update_time(sbi, REQ_TIME);
157
158out_sem:
159 filemap_invalidate_unlock_shared(inode->i_mapping);
160
161 sb_end_pagefault(inode->i_sb);
162out:
163 ret = vmf_fs_error(err);
164
165 trace_f2fs_vm_page_mkwrite(inode, folio->index, vmf->vma->vm_flags, ret);
166 return ret;
167}
168
169static const struct vm_operations_struct f2fs_file_vm_ops = {
170 .fault = f2fs_filemap_fault,
171 .map_pages = filemap_map_pages,
172 .page_mkwrite = f2fs_vm_page_mkwrite,
173};
174
175static int get_parent_ino(struct inode *inode, nid_t *pino)
176{
177 struct dentry *dentry;
178
179 /*
180 * Make sure to get the non-deleted alias. The alias associated with
181 * the open file descriptor being fsync()'ed may be deleted already.
182 */
183 dentry = d_find_alias(inode);
184 if (!dentry)
185 return 0;
186
187 *pino = d_parent_ino(dentry);
188 dput(dentry);
189 return 1;
190}
191
192static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
193{
194 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
195 enum cp_reason_type cp_reason = CP_NO_NEEDED;
196
197 if (!S_ISREG(inode->i_mode))
198 cp_reason = CP_NON_REGULAR;
199 else if (f2fs_compressed_file(inode))
200 cp_reason = CP_COMPRESSED;
201 else if (inode->i_nlink != 1)
202 cp_reason = CP_HARDLINK;
203 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
204 cp_reason = CP_SB_NEED_CP;
205 else if (file_wrong_pino(inode))
206 cp_reason = CP_WRONG_PINO;
207 else if (!f2fs_space_for_roll_forward(sbi))
208 cp_reason = CP_NO_SPC_ROLL;
209 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
210 cp_reason = CP_NODE_NEED_CP;
211 else if (test_opt(sbi, FASTBOOT))
212 cp_reason = CP_FASTBOOT_MODE;
213 else if (F2FS_OPTION(sbi).active_logs == 2)
214 cp_reason = CP_SPEC_LOG_NUM;
215 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
216 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
217 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
218 TRANS_DIR_INO))
219 cp_reason = CP_RECOVER_DIR;
220 else if (f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
221 XATTR_DIR_INO))
222 cp_reason = CP_XATTR_DIR;
223
224 return cp_reason;
225}
226
227static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
228{
229 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
230 bool ret = false;
231 /* But we need to avoid that there are some inode updates */
232 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
233 ret = true;
234 f2fs_put_page(i, 0);
235 return ret;
236}
237
238static void try_to_fix_pino(struct inode *inode)
239{
240 struct f2fs_inode_info *fi = F2FS_I(inode);
241 nid_t pino;
242
243 f2fs_down_write(&fi->i_sem);
244 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
245 get_parent_ino(inode, &pino)) {
246 f2fs_i_pino_write(inode, pino);
247 file_got_pino(inode);
248 }
249 f2fs_up_write(&fi->i_sem);
250}
251
252static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
253 int datasync, bool atomic)
254{
255 struct inode *inode = file->f_mapping->host;
256 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
257 nid_t ino = inode->i_ino;
258 int ret = 0;
259 enum cp_reason_type cp_reason = 0;
260 struct writeback_control wbc = {
261 .sync_mode = WB_SYNC_ALL,
262 .nr_to_write = LONG_MAX,
263 .for_reclaim = 0,
264 };
265 unsigned int seq_id = 0;
266
267 if (unlikely(f2fs_readonly(inode->i_sb)))
268 return 0;
269
270 trace_f2fs_sync_file_enter(inode);
271
272 if (S_ISDIR(inode->i_mode))
273 goto go_write;
274
275 /* if fdatasync is triggered, let's do in-place-update */
276 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
277 set_inode_flag(inode, FI_NEED_IPU);
278 ret = file_write_and_wait_range(file, start, end);
279 clear_inode_flag(inode, FI_NEED_IPU);
280
281 if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
282 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
283 return ret;
284 }
285
286 /* if the inode is dirty, let's recover all the time */
287 if (!f2fs_skip_inode_update(inode, datasync)) {
288 f2fs_write_inode(inode, NULL);
289 goto go_write;
290 }
291
292 /*
293 * if there is no written data, don't waste time to write recovery info.
294 */
295 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
296 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
297
298 /* it may call write_inode just prior to fsync */
299 if (need_inode_page_update(sbi, ino))
300 goto go_write;
301
302 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
303 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
304 goto flush_out;
305 goto out;
306 } else {
307 /*
308 * for OPU case, during fsync(), node can be persisted before
309 * data when lower device doesn't support write barrier, result
310 * in data corruption after SPO.
311 * So for strict fsync mode, force to use atomic write semantics
312 * to keep write order in between data/node and last node to
313 * avoid potential data corruption.
314 */
315 if (F2FS_OPTION(sbi).fsync_mode ==
316 FSYNC_MODE_STRICT && !atomic)
317 atomic = true;
318 }
319go_write:
320 /*
321 * Both of fdatasync() and fsync() are able to be recovered from
322 * sudden-power-off.
323 */
324 f2fs_down_read(&F2FS_I(inode)->i_sem);
325 cp_reason = need_do_checkpoint(inode);
326 f2fs_up_read(&F2FS_I(inode)->i_sem);
327
328 if (cp_reason) {
329 /* all the dirty node pages should be flushed for POR */
330 ret = f2fs_sync_fs(inode->i_sb, 1);
331
332 /*
333 * We've secured consistency through sync_fs. Following pino
334 * will be used only for fsynced inodes after checkpoint.
335 */
336 try_to_fix_pino(inode);
337 clear_inode_flag(inode, FI_APPEND_WRITE);
338 clear_inode_flag(inode, FI_UPDATE_WRITE);
339 goto out;
340 }
341sync_nodes:
342 atomic_inc(&sbi->wb_sync_req[NODE]);
343 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
344 atomic_dec(&sbi->wb_sync_req[NODE]);
345 if (ret)
346 goto out;
347
348 /* if cp_error was enabled, we should avoid infinite loop */
349 if (unlikely(f2fs_cp_error(sbi))) {
350 ret = -EIO;
351 goto out;
352 }
353
354 if (f2fs_need_inode_block_update(sbi, ino)) {
355 f2fs_mark_inode_dirty_sync(inode, true);
356 f2fs_write_inode(inode, NULL);
357 goto sync_nodes;
358 }
359
360 /*
361 * If it's atomic_write, it's just fine to keep write ordering. So
362 * here we don't need to wait for node write completion, since we use
363 * node chain which serializes node blocks. If one of node writes are
364 * reordered, we can see simply broken chain, resulting in stopping
365 * roll-forward recovery. It means we'll recover all or none node blocks
366 * given fsync mark.
367 */
368 if (!atomic) {
369 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
370 if (ret)
371 goto out;
372 }
373
374 /* once recovery info is written, don't need to tack this */
375 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
376 clear_inode_flag(inode, FI_APPEND_WRITE);
377flush_out:
378 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
379 ret = f2fs_issue_flush(sbi, inode->i_ino);
380 if (!ret) {
381 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
382 clear_inode_flag(inode, FI_UPDATE_WRITE);
383 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
384 }
385 f2fs_update_time(sbi, REQ_TIME);
386out:
387 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
388 return ret;
389}
390
391int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
392{
393 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
394 return -EIO;
395 return f2fs_do_sync_file(file, start, end, datasync, false);
396}
397
398static bool __found_offset(struct address_space *mapping,
399 struct dnode_of_data *dn, pgoff_t index, int whence)
400{
401 block_t blkaddr = f2fs_data_blkaddr(dn);
402 struct inode *inode = mapping->host;
403 bool compressed_cluster = false;
404
405 if (f2fs_compressed_file(inode)) {
406 block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_page,
407 ALIGN_DOWN(dn->ofs_in_node, F2FS_I(inode)->i_cluster_size));
408
409 compressed_cluster = first_blkaddr == COMPRESS_ADDR;
410 }
411
412 switch (whence) {
413 case SEEK_DATA:
414 if (__is_valid_data_blkaddr(blkaddr))
415 return true;
416 if (blkaddr == NEW_ADDR &&
417 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
418 return true;
419 if (compressed_cluster)
420 return true;
421 break;
422 case SEEK_HOLE:
423 if (compressed_cluster)
424 return false;
425 if (blkaddr == NULL_ADDR)
426 return true;
427 break;
428 }
429 return false;
430}
431
432static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
433{
434 struct inode *inode = file->f_mapping->host;
435 loff_t maxbytes = F2FS_BLK_TO_BYTES(max_file_blocks(inode));
436 struct dnode_of_data dn;
437 pgoff_t pgofs, end_offset;
438 loff_t data_ofs = offset;
439 loff_t isize;
440 int err = 0;
441
442 inode_lock_shared(inode);
443
444 isize = i_size_read(inode);
445 if (offset >= isize)
446 goto fail;
447
448 /* handle inline data case */
449 if (f2fs_has_inline_data(inode)) {
450 if (whence == SEEK_HOLE) {
451 data_ofs = isize;
452 goto found;
453 } else if (whence == SEEK_DATA) {
454 data_ofs = offset;
455 goto found;
456 }
457 }
458
459 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
460
461 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
462 set_new_dnode(&dn, inode, NULL, NULL, 0);
463 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
464 if (err && err != -ENOENT) {
465 goto fail;
466 } else if (err == -ENOENT) {
467 /* direct node does not exists */
468 if (whence == SEEK_DATA) {
469 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
470 continue;
471 } else {
472 goto found;
473 }
474 }
475
476 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
477
478 /* find data/hole in dnode block */
479 for (; dn.ofs_in_node < end_offset;
480 dn.ofs_in_node++, pgofs++,
481 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
482 block_t blkaddr;
483
484 blkaddr = f2fs_data_blkaddr(&dn);
485
486 if (__is_valid_data_blkaddr(blkaddr) &&
487 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
488 blkaddr, DATA_GENERIC_ENHANCE)) {
489 f2fs_put_dnode(&dn);
490 goto fail;
491 }
492
493 if (__found_offset(file->f_mapping, &dn,
494 pgofs, whence)) {
495 f2fs_put_dnode(&dn);
496 goto found;
497 }
498 }
499 f2fs_put_dnode(&dn);
500 }
501
502 if (whence == SEEK_DATA)
503 goto fail;
504found:
505 if (whence == SEEK_HOLE && data_ofs > isize)
506 data_ofs = isize;
507 inode_unlock_shared(inode);
508 return vfs_setpos(file, data_ofs, maxbytes);
509fail:
510 inode_unlock_shared(inode);
511 return -ENXIO;
512}
513
514static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
515{
516 struct inode *inode = file->f_mapping->host;
517 loff_t maxbytes = F2FS_BLK_TO_BYTES(max_file_blocks(inode));
518
519 switch (whence) {
520 case SEEK_SET:
521 case SEEK_CUR:
522 case SEEK_END:
523 return generic_file_llseek_size(file, offset, whence,
524 maxbytes, i_size_read(inode));
525 case SEEK_DATA:
526 case SEEK_HOLE:
527 if (offset < 0)
528 return -ENXIO;
529 return f2fs_seek_block(file, offset, whence);
530 }
531
532 return -EINVAL;
533}
534
535static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
536{
537 struct inode *inode = file_inode(file);
538
539 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
540 return -EIO;
541
542 if (!f2fs_is_compress_backend_ready(inode))
543 return -EOPNOTSUPP;
544
545 file_accessed(file);
546 vma->vm_ops = &f2fs_file_vm_ops;
547
548 f2fs_down_read(&F2FS_I(inode)->i_sem);
549 set_inode_flag(inode, FI_MMAP_FILE);
550 f2fs_up_read(&F2FS_I(inode)->i_sem);
551
552 return 0;
553}
554
555static int finish_preallocate_blocks(struct inode *inode)
556{
557 int ret;
558
559 inode_lock(inode);
560 if (is_inode_flag_set(inode, FI_OPENED_FILE)) {
561 inode_unlock(inode);
562 return 0;
563 }
564
565 if (!file_should_truncate(inode)) {
566 set_inode_flag(inode, FI_OPENED_FILE);
567 inode_unlock(inode);
568 return 0;
569 }
570
571 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
572 filemap_invalidate_lock(inode->i_mapping);
573
574 truncate_setsize(inode, i_size_read(inode));
575 ret = f2fs_truncate(inode);
576
577 filemap_invalidate_unlock(inode->i_mapping);
578 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
579
580 if (!ret)
581 set_inode_flag(inode, FI_OPENED_FILE);
582
583 inode_unlock(inode);
584 if (ret)
585 return ret;
586
587 file_dont_truncate(inode);
588 return 0;
589}
590
591static int f2fs_file_open(struct inode *inode, struct file *filp)
592{
593 int err = fscrypt_file_open(inode, filp);
594
595 if (err)
596 return err;
597
598 if (!f2fs_is_compress_backend_ready(inode))
599 return -EOPNOTSUPP;
600
601 err = fsverity_file_open(inode, filp);
602 if (err)
603 return err;
604
605 filp->f_mode |= FMODE_NOWAIT;
606 filp->f_mode |= FMODE_CAN_ODIRECT;
607
608 err = dquot_file_open(inode, filp);
609 if (err)
610 return err;
611
612 return finish_preallocate_blocks(inode);
613}
614
615void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
616{
617 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
618 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
619 __le32 *addr;
620 bool compressed_cluster = false;
621 int cluster_index = 0, valid_blocks = 0;
622 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
623 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
624
625 addr = get_dnode_addr(dn->inode, dn->node_page) + ofs;
626
627 /* Assumption: truncation starts with cluster */
628 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
629 block_t blkaddr = le32_to_cpu(*addr);
630
631 if (f2fs_compressed_file(dn->inode) &&
632 !(cluster_index & (cluster_size - 1))) {
633 if (compressed_cluster)
634 f2fs_i_compr_blocks_update(dn->inode,
635 valid_blocks, false);
636 compressed_cluster = (blkaddr == COMPRESS_ADDR);
637 valid_blocks = 0;
638 }
639
640 if (blkaddr == NULL_ADDR)
641 continue;
642
643 f2fs_set_data_blkaddr(dn, NULL_ADDR);
644
645 if (__is_valid_data_blkaddr(blkaddr)) {
646 if (time_to_inject(sbi, FAULT_BLKADDR_CONSISTENCE))
647 continue;
648 if (!f2fs_is_valid_blkaddr_raw(sbi, blkaddr,
649 DATA_GENERIC_ENHANCE))
650 continue;
651 if (compressed_cluster)
652 valid_blocks++;
653 }
654
655 f2fs_invalidate_blocks(sbi, blkaddr);
656
657 if (!released || blkaddr != COMPRESS_ADDR)
658 nr_free++;
659 }
660
661 if (compressed_cluster)
662 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
663
664 if (nr_free) {
665 pgoff_t fofs;
666 /*
667 * once we invalidate valid blkaddr in range [ofs, ofs + count],
668 * we will invalidate all blkaddr in the whole range.
669 */
670 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
671 dn->inode) + ofs;
672 f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
673 f2fs_update_age_extent_cache_range(dn, fofs, len);
674 dec_valid_block_count(sbi, dn->inode, nr_free);
675 }
676 dn->ofs_in_node = ofs;
677
678 f2fs_update_time(sbi, REQ_TIME);
679 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
680 dn->ofs_in_node, nr_free);
681}
682
683static int truncate_partial_data_page(struct inode *inode, u64 from,
684 bool cache_only)
685{
686 loff_t offset = from & (PAGE_SIZE - 1);
687 pgoff_t index = from >> PAGE_SHIFT;
688 struct address_space *mapping = inode->i_mapping;
689 struct page *page;
690
691 if (!offset && !cache_only)
692 return 0;
693
694 if (cache_only) {
695 page = find_lock_page(mapping, index);
696 if (page && PageUptodate(page))
697 goto truncate_out;
698 f2fs_put_page(page, 1);
699 return 0;
700 }
701
702 page = f2fs_get_lock_data_page(inode, index, true);
703 if (IS_ERR(page))
704 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
705truncate_out:
706 f2fs_wait_on_page_writeback(page, DATA, true, true);
707 zero_user(page, offset, PAGE_SIZE - offset);
708
709 /* An encrypted inode should have a key and truncate the last page. */
710 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
711 if (!cache_only)
712 set_page_dirty(page);
713 f2fs_put_page(page, 1);
714 return 0;
715}
716
717int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
718{
719 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
720 struct dnode_of_data dn;
721 pgoff_t free_from;
722 int count = 0, err = 0;
723 struct page *ipage;
724 bool truncate_page = false;
725
726 trace_f2fs_truncate_blocks_enter(inode, from);
727
728 if (IS_DEVICE_ALIASING(inode) && from) {
729 err = -EINVAL;
730 goto out_err;
731 }
732
733 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
734
735 if (free_from >= max_file_blocks(inode))
736 goto free_partial;
737
738 if (lock)
739 f2fs_lock_op(sbi);
740
741 ipage = f2fs_get_node_page(sbi, inode->i_ino);
742 if (IS_ERR(ipage)) {
743 err = PTR_ERR(ipage);
744 goto out;
745 }
746
747 if (IS_DEVICE_ALIASING(inode)) {
748 struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
749 struct extent_info ei = et->largest;
750 unsigned int i;
751
752 for (i = 0; i < ei.len; i++)
753 f2fs_invalidate_blocks(sbi, ei.blk + i);
754
755 dec_valid_block_count(sbi, inode, ei.len);
756 f2fs_update_time(sbi, REQ_TIME);
757
758 f2fs_put_page(ipage, 1);
759 goto out;
760 }
761
762 if (f2fs_has_inline_data(inode)) {
763 f2fs_truncate_inline_inode(inode, ipage, from);
764 f2fs_put_page(ipage, 1);
765 truncate_page = true;
766 goto out;
767 }
768
769 set_new_dnode(&dn, inode, ipage, NULL, 0);
770 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
771 if (err) {
772 if (err == -ENOENT)
773 goto free_next;
774 goto out;
775 }
776
777 count = ADDRS_PER_PAGE(dn.node_page, inode);
778
779 count -= dn.ofs_in_node;
780 f2fs_bug_on(sbi, count < 0);
781
782 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
783 f2fs_truncate_data_blocks_range(&dn, count);
784 free_from += count;
785 }
786
787 f2fs_put_dnode(&dn);
788free_next:
789 err = f2fs_truncate_inode_blocks(inode, free_from);
790out:
791 if (lock)
792 f2fs_unlock_op(sbi);
793free_partial:
794 /* lastly zero out the first data page */
795 if (!err)
796 err = truncate_partial_data_page(inode, from, truncate_page);
797out_err:
798 trace_f2fs_truncate_blocks_exit(inode, err);
799 return err;
800}
801
802int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
803{
804 u64 free_from = from;
805 int err;
806
807#ifdef CONFIG_F2FS_FS_COMPRESSION
808 /*
809 * for compressed file, only support cluster size
810 * aligned truncation.
811 */
812 if (f2fs_compressed_file(inode))
813 free_from = round_up(from,
814 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
815#endif
816
817 err = f2fs_do_truncate_blocks(inode, free_from, lock);
818 if (err)
819 return err;
820
821#ifdef CONFIG_F2FS_FS_COMPRESSION
822 /*
823 * For compressed file, after release compress blocks, don't allow write
824 * direct, but we should allow write direct after truncate to zero.
825 */
826 if (f2fs_compressed_file(inode) && !free_from
827 && is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
828 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
829
830 if (from != free_from) {
831 err = f2fs_truncate_partial_cluster(inode, from, lock);
832 if (err)
833 return err;
834 }
835#endif
836
837 return 0;
838}
839
840int f2fs_truncate(struct inode *inode)
841{
842 int err;
843
844 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
845 return -EIO;
846
847 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
848 S_ISLNK(inode->i_mode)))
849 return 0;
850
851 trace_f2fs_truncate(inode);
852
853 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE))
854 return -EIO;
855
856 err = f2fs_dquot_initialize(inode);
857 if (err)
858 return err;
859
860 /* we should check inline_data size */
861 if (!f2fs_may_inline_data(inode)) {
862 err = f2fs_convert_inline_inode(inode);
863 if (err)
864 return err;
865 }
866
867 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
868 if (err)
869 return err;
870
871 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
872 f2fs_mark_inode_dirty_sync(inode, false);
873 return 0;
874}
875
876static bool f2fs_force_buffered_io(struct inode *inode, int rw)
877{
878 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
879
880 if (!fscrypt_dio_supported(inode))
881 return true;
882 if (fsverity_active(inode))
883 return true;
884 if (f2fs_compressed_file(inode))
885 return true;
886 /*
887 * only force direct read to use buffered IO, for direct write,
888 * it expects inline data conversion before committing IO.
889 */
890 if (f2fs_has_inline_data(inode) && rw == READ)
891 return true;
892
893 /* disallow direct IO if any of devices has unaligned blksize */
894 if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
895 return true;
896 /*
897 * for blkzoned device, fallback direct IO to buffered IO, so
898 * all IOs can be serialized by log-structured write.
899 */
900 if (f2fs_sb_has_blkzoned(sbi) && (rw == WRITE) &&
901 !f2fs_is_pinned_file(inode))
902 return true;
903 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
904 return true;
905
906 return false;
907}
908
909int f2fs_getattr(struct mnt_idmap *idmap, const struct path *path,
910 struct kstat *stat, u32 request_mask, unsigned int query_flags)
911{
912 struct inode *inode = d_inode(path->dentry);
913 struct f2fs_inode_info *fi = F2FS_I(inode);
914 struct f2fs_inode *ri = NULL;
915 unsigned int flags;
916
917 if (f2fs_has_extra_attr(inode) &&
918 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
919 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
920 stat->result_mask |= STATX_BTIME;
921 stat->btime.tv_sec = fi->i_crtime.tv_sec;
922 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
923 }
924
925 /*
926 * Return the DIO alignment restrictions if requested. We only return
927 * this information when requested, since on encrypted files it might
928 * take a fair bit of work to get if the file wasn't opened recently.
929 *
930 * f2fs sometimes supports DIO reads but not DIO writes. STATX_DIOALIGN
931 * cannot represent that, so in that case we report no DIO support.
932 */
933 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
934 unsigned int bsize = i_blocksize(inode);
935
936 stat->result_mask |= STATX_DIOALIGN;
937 if (!f2fs_force_buffered_io(inode, WRITE)) {
938 stat->dio_mem_align = bsize;
939 stat->dio_offset_align = bsize;
940 }
941 }
942
943 flags = fi->i_flags;
944 if (flags & F2FS_COMPR_FL)
945 stat->attributes |= STATX_ATTR_COMPRESSED;
946 if (flags & F2FS_APPEND_FL)
947 stat->attributes |= STATX_ATTR_APPEND;
948 if (IS_ENCRYPTED(inode))
949 stat->attributes |= STATX_ATTR_ENCRYPTED;
950 if (flags & F2FS_IMMUTABLE_FL)
951 stat->attributes |= STATX_ATTR_IMMUTABLE;
952 if (flags & F2FS_NODUMP_FL)
953 stat->attributes |= STATX_ATTR_NODUMP;
954 if (IS_VERITY(inode))
955 stat->attributes |= STATX_ATTR_VERITY;
956
957 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
958 STATX_ATTR_APPEND |
959 STATX_ATTR_ENCRYPTED |
960 STATX_ATTR_IMMUTABLE |
961 STATX_ATTR_NODUMP |
962 STATX_ATTR_VERITY);
963
964 generic_fillattr(idmap, request_mask, inode, stat);
965
966 /* we need to show initial sectors used for inline_data/dentries */
967 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
968 f2fs_has_inline_dentry(inode))
969 stat->blocks += (stat->size + 511) >> 9;
970
971 return 0;
972}
973
974#ifdef CONFIG_F2FS_FS_POSIX_ACL
975static void __setattr_copy(struct mnt_idmap *idmap,
976 struct inode *inode, const struct iattr *attr)
977{
978 unsigned int ia_valid = attr->ia_valid;
979
980 i_uid_update(idmap, attr, inode);
981 i_gid_update(idmap, attr, inode);
982 if (ia_valid & ATTR_ATIME)
983 inode_set_atime_to_ts(inode, attr->ia_atime);
984 if (ia_valid & ATTR_MTIME)
985 inode_set_mtime_to_ts(inode, attr->ia_mtime);
986 if (ia_valid & ATTR_CTIME)
987 inode_set_ctime_to_ts(inode, attr->ia_ctime);
988 if (ia_valid & ATTR_MODE) {
989 umode_t mode = attr->ia_mode;
990
991 if (!in_group_or_capable(idmap, inode, i_gid_into_vfsgid(idmap, inode)))
992 mode &= ~S_ISGID;
993 set_acl_inode(inode, mode);
994 }
995}
996#else
997#define __setattr_copy setattr_copy
998#endif
999
1000int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
1001 struct iattr *attr)
1002{
1003 struct inode *inode = d_inode(dentry);
1004 struct f2fs_inode_info *fi = F2FS_I(inode);
1005 int err;
1006
1007 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1008 return -EIO;
1009
1010 if (unlikely(IS_IMMUTABLE(inode)))
1011 return -EPERM;
1012
1013 if (unlikely(IS_APPEND(inode) &&
1014 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
1015 ATTR_GID | ATTR_TIMES_SET))))
1016 return -EPERM;
1017
1018 if ((attr->ia_valid & ATTR_SIZE)) {
1019 if (!f2fs_is_compress_backend_ready(inode) ||
1020 IS_DEVICE_ALIASING(inode))
1021 return -EOPNOTSUPP;
1022 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED) &&
1023 !IS_ALIGNED(attr->ia_size,
1024 F2FS_BLK_TO_BYTES(fi->i_cluster_size)))
1025 return -EINVAL;
1026 }
1027
1028 err = setattr_prepare(idmap, dentry, attr);
1029 if (err)
1030 return err;
1031
1032 err = fscrypt_prepare_setattr(dentry, attr);
1033 if (err)
1034 return err;
1035
1036 err = fsverity_prepare_setattr(dentry, attr);
1037 if (err)
1038 return err;
1039
1040 if (is_quota_modification(idmap, inode, attr)) {
1041 err = f2fs_dquot_initialize(inode);
1042 if (err)
1043 return err;
1044 }
1045 if (i_uid_needs_update(idmap, attr, inode) ||
1046 i_gid_needs_update(idmap, attr, inode)) {
1047 f2fs_lock_op(F2FS_I_SB(inode));
1048 err = dquot_transfer(idmap, inode, attr);
1049 if (err) {
1050 set_sbi_flag(F2FS_I_SB(inode),
1051 SBI_QUOTA_NEED_REPAIR);
1052 f2fs_unlock_op(F2FS_I_SB(inode));
1053 return err;
1054 }
1055 /*
1056 * update uid/gid under lock_op(), so that dquot and inode can
1057 * be updated atomically.
1058 */
1059 i_uid_update(idmap, attr, inode);
1060 i_gid_update(idmap, attr, inode);
1061 f2fs_mark_inode_dirty_sync(inode, true);
1062 f2fs_unlock_op(F2FS_I_SB(inode));
1063 }
1064
1065 if (attr->ia_valid & ATTR_SIZE) {
1066 loff_t old_size = i_size_read(inode);
1067
1068 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
1069 /*
1070 * should convert inline inode before i_size_write to
1071 * keep smaller than inline_data size with inline flag.
1072 */
1073 err = f2fs_convert_inline_inode(inode);
1074 if (err)
1075 return err;
1076 }
1077
1078 /*
1079 * wait for inflight dio, blocks should be removed after
1080 * IO completion.
1081 */
1082 if (attr->ia_size < old_size)
1083 inode_dio_wait(inode);
1084
1085 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
1086 filemap_invalidate_lock(inode->i_mapping);
1087
1088 truncate_setsize(inode, attr->ia_size);
1089
1090 if (attr->ia_size <= old_size)
1091 err = f2fs_truncate(inode);
1092 /*
1093 * do not trim all blocks after i_size if target size is
1094 * larger than i_size.
1095 */
1096 filemap_invalidate_unlock(inode->i_mapping);
1097 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
1098 if (err)
1099 return err;
1100
1101 spin_lock(&fi->i_size_lock);
1102 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1103 fi->last_disk_size = i_size_read(inode);
1104 spin_unlock(&fi->i_size_lock);
1105 }
1106
1107 __setattr_copy(idmap, inode, attr);
1108
1109 if (attr->ia_valid & ATTR_MODE) {
1110 err = posix_acl_chmod(idmap, dentry, f2fs_get_inode_mode(inode));
1111
1112 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
1113 if (!err)
1114 inode->i_mode = fi->i_acl_mode;
1115 clear_inode_flag(inode, FI_ACL_MODE);
1116 }
1117 }
1118
1119 /* file size may changed here */
1120 f2fs_mark_inode_dirty_sync(inode, true);
1121
1122 /* inode change will produce dirty node pages flushed by checkpoint */
1123 f2fs_balance_fs(F2FS_I_SB(inode), true);
1124
1125 return err;
1126}
1127
1128const struct inode_operations f2fs_file_inode_operations = {
1129 .getattr = f2fs_getattr,
1130 .setattr = f2fs_setattr,
1131 .get_inode_acl = f2fs_get_acl,
1132 .set_acl = f2fs_set_acl,
1133 .listxattr = f2fs_listxattr,
1134 .fiemap = f2fs_fiemap,
1135 .fileattr_get = f2fs_fileattr_get,
1136 .fileattr_set = f2fs_fileattr_set,
1137};
1138
1139static int fill_zero(struct inode *inode, pgoff_t index,
1140 loff_t start, loff_t len)
1141{
1142 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1143 struct page *page;
1144
1145 if (!len)
1146 return 0;
1147
1148 f2fs_balance_fs(sbi, true);
1149
1150 f2fs_lock_op(sbi);
1151 page = f2fs_get_new_data_page(inode, NULL, index, false);
1152 f2fs_unlock_op(sbi);
1153
1154 if (IS_ERR(page))
1155 return PTR_ERR(page);
1156
1157 f2fs_wait_on_page_writeback(page, DATA, true, true);
1158 zero_user(page, start, len);
1159 set_page_dirty(page);
1160 f2fs_put_page(page, 1);
1161 return 0;
1162}
1163
1164int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1165{
1166 int err;
1167
1168 while (pg_start < pg_end) {
1169 struct dnode_of_data dn;
1170 pgoff_t end_offset, count;
1171
1172 set_new_dnode(&dn, inode, NULL, NULL, 0);
1173 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1174 if (err) {
1175 if (err == -ENOENT) {
1176 pg_start = f2fs_get_next_page_offset(&dn,
1177 pg_start);
1178 continue;
1179 }
1180 return err;
1181 }
1182
1183 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1184 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1185
1186 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1187
1188 f2fs_truncate_data_blocks_range(&dn, count);
1189 f2fs_put_dnode(&dn);
1190
1191 pg_start += count;
1192 }
1193 return 0;
1194}
1195
1196static int f2fs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
1197{
1198 pgoff_t pg_start, pg_end;
1199 loff_t off_start, off_end;
1200 int ret;
1201
1202 ret = f2fs_convert_inline_inode(inode);
1203 if (ret)
1204 return ret;
1205
1206 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1207 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1208
1209 off_start = offset & (PAGE_SIZE - 1);
1210 off_end = (offset + len) & (PAGE_SIZE - 1);
1211
1212 if (pg_start == pg_end) {
1213 ret = fill_zero(inode, pg_start, off_start,
1214 off_end - off_start);
1215 if (ret)
1216 return ret;
1217 } else {
1218 if (off_start) {
1219 ret = fill_zero(inode, pg_start++, off_start,
1220 PAGE_SIZE - off_start);
1221 if (ret)
1222 return ret;
1223 }
1224 if (off_end) {
1225 ret = fill_zero(inode, pg_end, 0, off_end);
1226 if (ret)
1227 return ret;
1228 }
1229
1230 if (pg_start < pg_end) {
1231 loff_t blk_start, blk_end;
1232 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1233
1234 f2fs_balance_fs(sbi, true);
1235
1236 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1237 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1238
1239 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1240 filemap_invalidate_lock(inode->i_mapping);
1241
1242 truncate_pagecache_range(inode, blk_start, blk_end - 1);
1243
1244 f2fs_lock_op(sbi);
1245 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1246 f2fs_unlock_op(sbi);
1247
1248 filemap_invalidate_unlock(inode->i_mapping);
1249 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1250 }
1251 }
1252
1253 return ret;
1254}
1255
1256static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1257 int *do_replace, pgoff_t off, pgoff_t len)
1258{
1259 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1260 struct dnode_of_data dn;
1261 int ret, done, i;
1262
1263next_dnode:
1264 set_new_dnode(&dn, inode, NULL, NULL, 0);
1265 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1266 if (ret && ret != -ENOENT) {
1267 return ret;
1268 } else if (ret == -ENOENT) {
1269 if (dn.max_level == 0)
1270 return -ENOENT;
1271 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1272 dn.ofs_in_node, len);
1273 blkaddr += done;
1274 do_replace += done;
1275 goto next;
1276 }
1277
1278 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1279 dn.ofs_in_node, len);
1280 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1281 *blkaddr = f2fs_data_blkaddr(&dn);
1282
1283 if (__is_valid_data_blkaddr(*blkaddr) &&
1284 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1285 DATA_GENERIC_ENHANCE)) {
1286 f2fs_put_dnode(&dn);
1287 return -EFSCORRUPTED;
1288 }
1289
1290 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1291
1292 if (f2fs_lfs_mode(sbi)) {
1293 f2fs_put_dnode(&dn);
1294 return -EOPNOTSUPP;
1295 }
1296
1297 /* do not invalidate this block address */
1298 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1299 *do_replace = 1;
1300 }
1301 }
1302 f2fs_put_dnode(&dn);
1303next:
1304 len -= done;
1305 off += done;
1306 if (len)
1307 goto next_dnode;
1308 return 0;
1309}
1310
1311static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1312 int *do_replace, pgoff_t off, int len)
1313{
1314 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1315 struct dnode_of_data dn;
1316 int ret, i;
1317
1318 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1319 if (*do_replace == 0)
1320 continue;
1321
1322 set_new_dnode(&dn, inode, NULL, NULL, 0);
1323 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1324 if (ret) {
1325 dec_valid_block_count(sbi, inode, 1);
1326 f2fs_invalidate_blocks(sbi, *blkaddr);
1327 } else {
1328 f2fs_update_data_blkaddr(&dn, *blkaddr);
1329 }
1330 f2fs_put_dnode(&dn);
1331 }
1332 return 0;
1333}
1334
1335static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1336 block_t *blkaddr, int *do_replace,
1337 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1338{
1339 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1340 pgoff_t i = 0;
1341 int ret;
1342
1343 while (i < len) {
1344 if (blkaddr[i] == NULL_ADDR && !full) {
1345 i++;
1346 continue;
1347 }
1348
1349 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1350 struct dnode_of_data dn;
1351 struct node_info ni;
1352 size_t new_size;
1353 pgoff_t ilen;
1354
1355 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1356 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1357 if (ret)
1358 return ret;
1359
1360 ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
1361 if (ret) {
1362 f2fs_put_dnode(&dn);
1363 return ret;
1364 }
1365
1366 ilen = min((pgoff_t)
1367 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1368 dn.ofs_in_node, len - i);
1369 do {
1370 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1371 f2fs_truncate_data_blocks_range(&dn, 1);
1372
1373 if (do_replace[i]) {
1374 f2fs_i_blocks_write(src_inode,
1375 1, false, false);
1376 f2fs_i_blocks_write(dst_inode,
1377 1, true, false);
1378 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1379 blkaddr[i], ni.version, true, false);
1380
1381 do_replace[i] = 0;
1382 }
1383 dn.ofs_in_node++;
1384 i++;
1385 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1386 if (dst_inode->i_size < new_size)
1387 f2fs_i_size_write(dst_inode, new_size);
1388 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1389
1390 f2fs_put_dnode(&dn);
1391 } else {
1392 struct page *psrc, *pdst;
1393
1394 psrc = f2fs_get_lock_data_page(src_inode,
1395 src + i, true);
1396 if (IS_ERR(psrc))
1397 return PTR_ERR(psrc);
1398 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1399 true);
1400 if (IS_ERR(pdst)) {
1401 f2fs_put_page(psrc, 1);
1402 return PTR_ERR(pdst);
1403 }
1404
1405 f2fs_wait_on_page_writeback(pdst, DATA, true, true);
1406
1407 memcpy_page(pdst, 0, psrc, 0, PAGE_SIZE);
1408 set_page_dirty(pdst);
1409 set_page_private_gcing(pdst);
1410 f2fs_put_page(pdst, 1);
1411 f2fs_put_page(psrc, 1);
1412
1413 ret = f2fs_truncate_hole(src_inode,
1414 src + i, src + i + 1);
1415 if (ret)
1416 return ret;
1417 i++;
1418 }
1419 }
1420 return 0;
1421}
1422
1423static int __exchange_data_block(struct inode *src_inode,
1424 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1425 pgoff_t len, bool full)
1426{
1427 block_t *src_blkaddr;
1428 int *do_replace;
1429 pgoff_t olen;
1430 int ret;
1431
1432 while (len) {
1433 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1434
1435 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1436 array_size(olen, sizeof(block_t)),
1437 GFP_NOFS);
1438 if (!src_blkaddr)
1439 return -ENOMEM;
1440
1441 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1442 array_size(olen, sizeof(int)),
1443 GFP_NOFS);
1444 if (!do_replace) {
1445 kvfree(src_blkaddr);
1446 return -ENOMEM;
1447 }
1448
1449 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1450 do_replace, src, olen);
1451 if (ret)
1452 goto roll_back;
1453
1454 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1455 do_replace, src, dst, olen, full);
1456 if (ret)
1457 goto roll_back;
1458
1459 src += olen;
1460 dst += olen;
1461 len -= olen;
1462
1463 kvfree(src_blkaddr);
1464 kvfree(do_replace);
1465 }
1466 return 0;
1467
1468roll_back:
1469 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1470 kvfree(src_blkaddr);
1471 kvfree(do_replace);
1472 return ret;
1473}
1474
1475static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1476{
1477 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1478 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1479 pgoff_t start = offset >> PAGE_SHIFT;
1480 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1481 int ret;
1482
1483 f2fs_balance_fs(sbi, true);
1484
1485 /* avoid gc operation during block exchange */
1486 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1487 filemap_invalidate_lock(inode->i_mapping);
1488
1489 f2fs_lock_op(sbi);
1490 f2fs_drop_extent_tree(inode);
1491 truncate_pagecache(inode, offset);
1492 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1493 f2fs_unlock_op(sbi);
1494
1495 filemap_invalidate_unlock(inode->i_mapping);
1496 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1497 return ret;
1498}
1499
1500static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1501{
1502 loff_t new_size;
1503 int ret;
1504
1505 if (offset + len >= i_size_read(inode))
1506 return -EINVAL;
1507
1508 /* collapse range should be aligned to block size of f2fs. */
1509 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1510 return -EINVAL;
1511
1512 ret = f2fs_convert_inline_inode(inode);
1513 if (ret)
1514 return ret;
1515
1516 /* write out all dirty pages from offset */
1517 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1518 if (ret)
1519 return ret;
1520
1521 ret = f2fs_do_collapse(inode, offset, len);
1522 if (ret)
1523 return ret;
1524
1525 /* write out all moved pages, if possible */
1526 filemap_invalidate_lock(inode->i_mapping);
1527 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1528 truncate_pagecache(inode, offset);
1529
1530 new_size = i_size_read(inode) - len;
1531 ret = f2fs_truncate_blocks(inode, new_size, true);
1532 filemap_invalidate_unlock(inode->i_mapping);
1533 if (!ret)
1534 f2fs_i_size_write(inode, new_size);
1535 return ret;
1536}
1537
1538static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1539 pgoff_t end)
1540{
1541 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1542 pgoff_t index = start;
1543 unsigned int ofs_in_node = dn->ofs_in_node;
1544 blkcnt_t count = 0;
1545 int ret;
1546
1547 for (; index < end; index++, dn->ofs_in_node++) {
1548 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1549 count++;
1550 }
1551
1552 dn->ofs_in_node = ofs_in_node;
1553 ret = f2fs_reserve_new_blocks(dn, count);
1554 if (ret)
1555 return ret;
1556
1557 dn->ofs_in_node = ofs_in_node;
1558 for (index = start; index < end; index++, dn->ofs_in_node++) {
1559 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1560 /*
1561 * f2fs_reserve_new_blocks will not guarantee entire block
1562 * allocation.
1563 */
1564 if (dn->data_blkaddr == NULL_ADDR) {
1565 ret = -ENOSPC;
1566 break;
1567 }
1568
1569 if (dn->data_blkaddr == NEW_ADDR)
1570 continue;
1571
1572 if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
1573 DATA_GENERIC_ENHANCE)) {
1574 ret = -EFSCORRUPTED;
1575 break;
1576 }
1577
1578 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1579 f2fs_set_data_blkaddr(dn, NEW_ADDR);
1580 }
1581
1582 f2fs_update_read_extent_cache_range(dn, start, 0, index - start);
1583 f2fs_update_age_extent_cache_range(dn, start, index - start);
1584
1585 return ret;
1586}
1587
1588static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1589 int mode)
1590{
1591 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1592 struct address_space *mapping = inode->i_mapping;
1593 pgoff_t index, pg_start, pg_end;
1594 loff_t new_size = i_size_read(inode);
1595 loff_t off_start, off_end;
1596 int ret = 0;
1597
1598 ret = inode_newsize_ok(inode, (len + offset));
1599 if (ret)
1600 return ret;
1601
1602 ret = f2fs_convert_inline_inode(inode);
1603 if (ret)
1604 return ret;
1605
1606 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1607 if (ret)
1608 return ret;
1609
1610 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1611 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1612
1613 off_start = offset & (PAGE_SIZE - 1);
1614 off_end = (offset + len) & (PAGE_SIZE - 1);
1615
1616 if (pg_start == pg_end) {
1617 ret = fill_zero(inode, pg_start, off_start,
1618 off_end - off_start);
1619 if (ret)
1620 return ret;
1621
1622 new_size = max_t(loff_t, new_size, offset + len);
1623 } else {
1624 if (off_start) {
1625 ret = fill_zero(inode, pg_start++, off_start,
1626 PAGE_SIZE - off_start);
1627 if (ret)
1628 return ret;
1629
1630 new_size = max_t(loff_t, new_size,
1631 (loff_t)pg_start << PAGE_SHIFT);
1632 }
1633
1634 for (index = pg_start; index < pg_end;) {
1635 struct dnode_of_data dn;
1636 unsigned int end_offset;
1637 pgoff_t end;
1638
1639 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1640 filemap_invalidate_lock(mapping);
1641
1642 truncate_pagecache_range(inode,
1643 (loff_t)index << PAGE_SHIFT,
1644 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1645
1646 f2fs_lock_op(sbi);
1647
1648 set_new_dnode(&dn, inode, NULL, NULL, 0);
1649 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1650 if (ret) {
1651 f2fs_unlock_op(sbi);
1652 filemap_invalidate_unlock(mapping);
1653 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1654 goto out;
1655 }
1656
1657 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1658 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1659
1660 ret = f2fs_do_zero_range(&dn, index, end);
1661 f2fs_put_dnode(&dn);
1662
1663 f2fs_unlock_op(sbi);
1664 filemap_invalidate_unlock(mapping);
1665 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1666
1667 f2fs_balance_fs(sbi, dn.node_changed);
1668
1669 if (ret)
1670 goto out;
1671
1672 index = end;
1673 new_size = max_t(loff_t, new_size,
1674 (loff_t)index << PAGE_SHIFT);
1675 }
1676
1677 if (off_end) {
1678 ret = fill_zero(inode, pg_end, 0, off_end);
1679 if (ret)
1680 goto out;
1681
1682 new_size = max_t(loff_t, new_size, offset + len);
1683 }
1684 }
1685
1686out:
1687 if (new_size > i_size_read(inode)) {
1688 if (mode & FALLOC_FL_KEEP_SIZE)
1689 file_set_keep_isize(inode);
1690 else
1691 f2fs_i_size_write(inode, new_size);
1692 }
1693 return ret;
1694}
1695
1696static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1697{
1698 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1699 struct address_space *mapping = inode->i_mapping;
1700 pgoff_t nr, pg_start, pg_end, delta, idx;
1701 loff_t new_size;
1702 int ret = 0;
1703
1704 new_size = i_size_read(inode) + len;
1705 ret = inode_newsize_ok(inode, new_size);
1706 if (ret)
1707 return ret;
1708
1709 if (offset >= i_size_read(inode))
1710 return -EINVAL;
1711
1712 /* insert range should be aligned to block size of f2fs. */
1713 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1714 return -EINVAL;
1715
1716 ret = f2fs_convert_inline_inode(inode);
1717 if (ret)
1718 return ret;
1719
1720 f2fs_balance_fs(sbi, true);
1721
1722 filemap_invalidate_lock(mapping);
1723 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1724 filemap_invalidate_unlock(mapping);
1725 if (ret)
1726 return ret;
1727
1728 /* write out all dirty pages from offset */
1729 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1730 if (ret)
1731 return ret;
1732
1733 pg_start = offset >> PAGE_SHIFT;
1734 pg_end = (offset + len) >> PAGE_SHIFT;
1735 delta = pg_end - pg_start;
1736 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1737
1738 /* avoid gc operation during block exchange */
1739 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1740 filemap_invalidate_lock(mapping);
1741 truncate_pagecache(inode, offset);
1742
1743 while (!ret && idx > pg_start) {
1744 nr = idx - pg_start;
1745 if (nr > delta)
1746 nr = delta;
1747 idx -= nr;
1748
1749 f2fs_lock_op(sbi);
1750 f2fs_drop_extent_tree(inode);
1751
1752 ret = __exchange_data_block(inode, inode, idx,
1753 idx + delta, nr, false);
1754 f2fs_unlock_op(sbi);
1755 }
1756 filemap_invalidate_unlock(mapping);
1757 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1758 if (ret)
1759 return ret;
1760
1761 /* write out all moved pages, if possible */
1762 filemap_invalidate_lock(mapping);
1763 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1764 truncate_pagecache(inode, offset);
1765 filemap_invalidate_unlock(mapping);
1766
1767 if (!ret)
1768 f2fs_i_size_write(inode, new_size);
1769 return ret;
1770}
1771
1772static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
1773 loff_t len, int mode)
1774{
1775 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1776 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1777 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1778 .m_may_create = true };
1779 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1780 .init_gc_type = FG_GC,
1781 .should_migrate_blocks = false,
1782 .err_gc_skipped = true,
1783 .nr_free_secs = 0 };
1784 pgoff_t pg_start, pg_end;
1785 loff_t new_size;
1786 loff_t off_end;
1787 block_t expanded = 0;
1788 int err;
1789
1790 err = inode_newsize_ok(inode, (len + offset));
1791 if (err)
1792 return err;
1793
1794 err = f2fs_convert_inline_inode(inode);
1795 if (err)
1796 return err;
1797
1798 f2fs_balance_fs(sbi, true);
1799
1800 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1801 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1802 off_end = (offset + len) & (PAGE_SIZE - 1);
1803
1804 map.m_lblk = pg_start;
1805 map.m_len = pg_end - pg_start;
1806 if (off_end)
1807 map.m_len++;
1808
1809 if (!map.m_len)
1810 return 0;
1811
1812 if (f2fs_is_pinned_file(inode)) {
1813 block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
1814 block_t sec_len = roundup(map.m_len, sec_blks);
1815
1816 map.m_len = sec_blks;
1817next_alloc:
1818 if (has_not_enough_free_secs(sbi, 0, f2fs_sb_has_blkzoned(sbi) ?
1819 ZONED_PIN_SEC_REQUIRED_COUNT :
1820 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1821 f2fs_down_write(&sbi->gc_lock);
1822 stat_inc_gc_call_count(sbi, FOREGROUND);
1823 err = f2fs_gc(sbi, &gc_control);
1824 if (err && err != -ENODATA)
1825 goto out_err;
1826 }
1827
1828 f2fs_down_write(&sbi->pin_sem);
1829
1830 err = f2fs_allocate_pinning_section(sbi);
1831 if (err) {
1832 f2fs_up_write(&sbi->pin_sem);
1833 goto out_err;
1834 }
1835
1836 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1837 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_DIO);
1838 file_dont_truncate(inode);
1839
1840 f2fs_up_write(&sbi->pin_sem);
1841
1842 expanded += map.m_len;
1843 sec_len -= map.m_len;
1844 map.m_lblk += map.m_len;
1845 if (!err && sec_len)
1846 goto next_alloc;
1847
1848 map.m_len = expanded;
1849 } else {
1850 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_AIO);
1851 expanded = map.m_len;
1852 }
1853out_err:
1854 if (err) {
1855 pgoff_t last_off;
1856
1857 if (!expanded)
1858 return err;
1859
1860 last_off = pg_start + expanded - 1;
1861
1862 /* update new size to the failed position */
1863 new_size = (last_off == pg_end) ? offset + len :
1864 (loff_t)(last_off + 1) << PAGE_SHIFT;
1865 } else {
1866 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1867 }
1868
1869 if (new_size > i_size_read(inode)) {
1870 if (mode & FALLOC_FL_KEEP_SIZE)
1871 file_set_keep_isize(inode);
1872 else
1873 f2fs_i_size_write(inode, new_size);
1874 }
1875
1876 return err;
1877}
1878
1879static long f2fs_fallocate(struct file *file, int mode,
1880 loff_t offset, loff_t len)
1881{
1882 struct inode *inode = file_inode(file);
1883 long ret = 0;
1884
1885 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1886 return -EIO;
1887 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1888 return -ENOSPC;
1889 if (!f2fs_is_compress_backend_ready(inode) || IS_DEVICE_ALIASING(inode))
1890 return -EOPNOTSUPP;
1891
1892 /* f2fs only support ->fallocate for regular file */
1893 if (!S_ISREG(inode->i_mode))
1894 return -EINVAL;
1895
1896 if (IS_ENCRYPTED(inode) &&
1897 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1898 return -EOPNOTSUPP;
1899
1900 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1901 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1902 FALLOC_FL_INSERT_RANGE))
1903 return -EOPNOTSUPP;
1904
1905 inode_lock(inode);
1906
1907 /*
1908 * Pinned file should not support partial truncation since the block
1909 * can be used by applications.
1910 */
1911 if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
1912 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1913 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
1914 ret = -EOPNOTSUPP;
1915 goto out;
1916 }
1917
1918 ret = file_modified(file);
1919 if (ret)
1920 goto out;
1921
1922 /*
1923 * wait for inflight dio, blocks should be removed after IO
1924 * completion.
1925 */
1926 inode_dio_wait(inode);
1927
1928 if (mode & FALLOC_FL_PUNCH_HOLE) {
1929 if (offset >= inode->i_size)
1930 goto out;
1931
1932 ret = f2fs_punch_hole(inode, offset, len);
1933 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1934 ret = f2fs_collapse_range(inode, offset, len);
1935 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1936 ret = f2fs_zero_range(inode, offset, len, mode);
1937 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1938 ret = f2fs_insert_range(inode, offset, len);
1939 } else {
1940 ret = f2fs_expand_inode_data(inode, offset, len, mode);
1941 }
1942
1943 if (!ret) {
1944 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1945 f2fs_mark_inode_dirty_sync(inode, false);
1946 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1947 }
1948
1949out:
1950 inode_unlock(inode);
1951
1952 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1953 return ret;
1954}
1955
1956static int f2fs_release_file(struct inode *inode, struct file *filp)
1957{
1958 /*
1959 * f2fs_release_file is called at every close calls. So we should
1960 * not drop any inmemory pages by close called by other process.
1961 */
1962 if (!(filp->f_mode & FMODE_WRITE) ||
1963 atomic_read(&inode->i_writecount) != 1)
1964 return 0;
1965
1966 inode_lock(inode);
1967 f2fs_abort_atomic_write(inode, true);
1968 inode_unlock(inode);
1969
1970 return 0;
1971}
1972
1973static int f2fs_file_flush(struct file *file, fl_owner_t id)
1974{
1975 struct inode *inode = file_inode(file);
1976
1977 /*
1978 * If the process doing a transaction is crashed, we should do
1979 * roll-back. Otherwise, other reader/write can see corrupted database
1980 * until all the writers close its file. Since this should be done
1981 * before dropping file lock, it needs to do in ->flush.
1982 */
1983 if (F2FS_I(inode)->atomic_write_task == current &&
1984 (current->flags & PF_EXITING)) {
1985 inode_lock(inode);
1986 f2fs_abort_atomic_write(inode, true);
1987 inode_unlock(inode);
1988 }
1989
1990 return 0;
1991}
1992
1993static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1994{
1995 struct f2fs_inode_info *fi = F2FS_I(inode);
1996 u32 masked_flags = fi->i_flags & mask;
1997
1998 /* mask can be shrunk by flags_valid selector */
1999 iflags &= mask;
2000
2001 /* Is it quota file? Do not allow user to mess with it */
2002 if (IS_NOQUOTA(inode))
2003 return -EPERM;
2004
2005 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
2006 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
2007 return -EOPNOTSUPP;
2008 if (!f2fs_empty_dir(inode))
2009 return -ENOTEMPTY;
2010 }
2011
2012 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
2013 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
2014 return -EOPNOTSUPP;
2015 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
2016 return -EINVAL;
2017 }
2018
2019 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
2020 if (masked_flags & F2FS_COMPR_FL) {
2021 if (!f2fs_disable_compressed_file(inode))
2022 return -EINVAL;
2023 } else {
2024 /* try to convert inline_data to support compression */
2025 int err = f2fs_convert_inline_inode(inode);
2026 if (err)
2027 return err;
2028
2029 f2fs_down_write(&fi->i_sem);
2030 if (!f2fs_may_compress(inode) ||
2031 (S_ISREG(inode->i_mode) &&
2032 F2FS_HAS_BLOCKS(inode))) {
2033 f2fs_up_write(&fi->i_sem);
2034 return -EINVAL;
2035 }
2036 err = set_compress_context(inode);
2037 f2fs_up_write(&fi->i_sem);
2038
2039 if (err)
2040 return err;
2041 }
2042 }
2043
2044 fi->i_flags = iflags | (fi->i_flags & ~mask);
2045 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
2046 (fi->i_flags & F2FS_NOCOMP_FL));
2047
2048 if (fi->i_flags & F2FS_PROJINHERIT_FL)
2049 set_inode_flag(inode, FI_PROJ_INHERIT);
2050 else
2051 clear_inode_flag(inode, FI_PROJ_INHERIT);
2052
2053 inode_set_ctime_current(inode);
2054 f2fs_set_inode_flags(inode);
2055 f2fs_mark_inode_dirty_sync(inode, true);
2056 return 0;
2057}
2058
2059/* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
2060
2061/*
2062 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
2063 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
2064 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
2065 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
2066 *
2067 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
2068 * FS_IOC_FSSETXATTR is done by the VFS.
2069 */
2070
2071static const struct {
2072 u32 iflag;
2073 u32 fsflag;
2074} f2fs_fsflags_map[] = {
2075 { F2FS_COMPR_FL, FS_COMPR_FL },
2076 { F2FS_SYNC_FL, FS_SYNC_FL },
2077 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
2078 { F2FS_APPEND_FL, FS_APPEND_FL },
2079 { F2FS_NODUMP_FL, FS_NODUMP_FL },
2080 { F2FS_NOATIME_FL, FS_NOATIME_FL },
2081 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
2082 { F2FS_INDEX_FL, FS_INDEX_FL },
2083 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
2084 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
2085 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
2086};
2087
2088#define F2FS_GETTABLE_FS_FL ( \
2089 FS_COMPR_FL | \
2090 FS_SYNC_FL | \
2091 FS_IMMUTABLE_FL | \
2092 FS_APPEND_FL | \
2093 FS_NODUMP_FL | \
2094 FS_NOATIME_FL | \
2095 FS_NOCOMP_FL | \
2096 FS_INDEX_FL | \
2097 FS_DIRSYNC_FL | \
2098 FS_PROJINHERIT_FL | \
2099 FS_ENCRYPT_FL | \
2100 FS_INLINE_DATA_FL | \
2101 FS_NOCOW_FL | \
2102 FS_VERITY_FL | \
2103 FS_CASEFOLD_FL)
2104
2105#define F2FS_SETTABLE_FS_FL ( \
2106 FS_COMPR_FL | \
2107 FS_SYNC_FL | \
2108 FS_IMMUTABLE_FL | \
2109 FS_APPEND_FL | \
2110 FS_NODUMP_FL | \
2111 FS_NOATIME_FL | \
2112 FS_NOCOMP_FL | \
2113 FS_DIRSYNC_FL | \
2114 FS_PROJINHERIT_FL | \
2115 FS_CASEFOLD_FL)
2116
2117/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
2118static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
2119{
2120 u32 fsflags = 0;
2121 int i;
2122
2123 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2124 if (iflags & f2fs_fsflags_map[i].iflag)
2125 fsflags |= f2fs_fsflags_map[i].fsflag;
2126
2127 return fsflags;
2128}
2129
2130/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
2131static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
2132{
2133 u32 iflags = 0;
2134 int i;
2135
2136 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2137 if (fsflags & f2fs_fsflags_map[i].fsflag)
2138 iflags |= f2fs_fsflags_map[i].iflag;
2139
2140 return iflags;
2141}
2142
2143static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2144{
2145 struct inode *inode = file_inode(filp);
2146
2147 return put_user(inode->i_generation, (int __user *)arg);
2148}
2149
2150static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
2151{
2152 struct inode *inode = file_inode(filp);
2153 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2154 struct f2fs_inode_info *fi = F2FS_I(inode);
2155 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2156 loff_t isize;
2157 int ret;
2158
2159 if (!(filp->f_mode & FMODE_WRITE))
2160 return -EBADF;
2161
2162 if (!inode_owner_or_capable(idmap, inode))
2163 return -EACCES;
2164
2165 if (!S_ISREG(inode->i_mode))
2166 return -EINVAL;
2167
2168 if (filp->f_flags & O_DIRECT)
2169 return -EINVAL;
2170
2171 ret = mnt_want_write_file(filp);
2172 if (ret)
2173 return ret;
2174
2175 inode_lock(inode);
2176
2177 if (!f2fs_disable_compressed_file(inode) ||
2178 f2fs_is_pinned_file(inode)) {
2179 ret = -EINVAL;
2180 goto out;
2181 }
2182
2183 if (f2fs_is_atomic_file(inode))
2184 goto out;
2185
2186 ret = f2fs_convert_inline_inode(inode);
2187 if (ret)
2188 goto out;
2189
2190 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
2191 f2fs_down_write(&fi->i_gc_rwsem[READ]);
2192
2193 /*
2194 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2195 * f2fs_is_atomic_file.
2196 */
2197 if (get_dirty_pages(inode))
2198 f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2199 inode->i_ino, get_dirty_pages(inode));
2200 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2201 if (ret)
2202 goto out_unlock;
2203
2204 /* Check if the inode already has a COW inode */
2205 if (fi->cow_inode == NULL) {
2206 /* Create a COW inode for atomic write */
2207 struct dentry *dentry = file_dentry(filp);
2208 struct inode *dir = d_inode(dentry->d_parent);
2209
2210 ret = f2fs_get_tmpfile(idmap, dir, &fi->cow_inode);
2211 if (ret)
2212 goto out_unlock;
2213
2214 set_inode_flag(fi->cow_inode, FI_COW_FILE);
2215 clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
2216
2217 /* Set the COW inode's atomic_inode to the atomic inode */
2218 F2FS_I(fi->cow_inode)->atomic_inode = inode;
2219 } else {
2220 /* Reuse the already created COW inode */
2221 f2fs_bug_on(sbi, get_dirty_pages(fi->cow_inode));
2222
2223 invalidate_mapping_pages(fi->cow_inode->i_mapping, 0, -1);
2224
2225 ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
2226 if (ret)
2227 goto out_unlock;
2228 }
2229
2230 f2fs_write_inode(inode, NULL);
2231
2232 stat_inc_atomic_inode(inode);
2233
2234 set_inode_flag(inode, FI_ATOMIC_FILE);
2235
2236 isize = i_size_read(inode);
2237 fi->original_i_size = isize;
2238 if (truncate) {
2239 set_inode_flag(inode, FI_ATOMIC_REPLACE);
2240 truncate_inode_pages_final(inode->i_mapping);
2241 f2fs_i_size_write(inode, 0);
2242 isize = 0;
2243 }
2244 f2fs_i_size_write(fi->cow_inode, isize);
2245
2246out_unlock:
2247 f2fs_up_write(&fi->i_gc_rwsem[READ]);
2248 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2249 if (ret)
2250 goto out;
2251
2252 f2fs_update_time(sbi, REQ_TIME);
2253 fi->atomic_write_task = current;
2254 stat_update_max_atomic_write(inode);
2255 fi->atomic_write_cnt = 0;
2256out:
2257 inode_unlock(inode);
2258 mnt_drop_write_file(filp);
2259 return ret;
2260}
2261
2262static int f2fs_ioc_commit_atomic_write(struct file *filp)
2263{
2264 struct inode *inode = file_inode(filp);
2265 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2266 int ret;
2267
2268 if (!(filp->f_mode & FMODE_WRITE))
2269 return -EBADF;
2270
2271 if (!inode_owner_or_capable(idmap, inode))
2272 return -EACCES;
2273
2274 ret = mnt_want_write_file(filp);
2275 if (ret)
2276 return ret;
2277
2278 f2fs_balance_fs(F2FS_I_SB(inode), true);
2279
2280 inode_lock(inode);
2281
2282 if (f2fs_is_atomic_file(inode)) {
2283 ret = f2fs_commit_atomic_write(inode);
2284 if (!ret)
2285 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2286
2287 f2fs_abort_atomic_write(inode, ret);
2288 } else {
2289 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2290 }
2291
2292 inode_unlock(inode);
2293 mnt_drop_write_file(filp);
2294 return ret;
2295}
2296
2297static int f2fs_ioc_abort_atomic_write(struct file *filp)
2298{
2299 struct inode *inode = file_inode(filp);
2300 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2301 int ret;
2302
2303 if (!(filp->f_mode & FMODE_WRITE))
2304 return -EBADF;
2305
2306 if (!inode_owner_or_capable(idmap, inode))
2307 return -EACCES;
2308
2309 ret = mnt_want_write_file(filp);
2310 if (ret)
2311 return ret;
2312
2313 inode_lock(inode);
2314
2315 f2fs_abort_atomic_write(inode, true);
2316
2317 inode_unlock(inode);
2318
2319 mnt_drop_write_file(filp);
2320 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2321 return ret;
2322}
2323
2324int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
2325 bool readonly, bool need_lock)
2326{
2327 struct super_block *sb = sbi->sb;
2328 int ret = 0;
2329
2330 switch (flag) {
2331 case F2FS_GOING_DOWN_FULLSYNC:
2332 ret = bdev_freeze(sb->s_bdev);
2333 if (ret)
2334 goto out;
2335 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2336 bdev_thaw(sb->s_bdev);
2337 break;
2338 case F2FS_GOING_DOWN_METASYNC:
2339 /* do checkpoint only */
2340 ret = f2fs_sync_fs(sb, 1);
2341 if (ret) {
2342 if (ret == -EIO)
2343 ret = 0;
2344 goto out;
2345 }
2346 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2347 break;
2348 case F2FS_GOING_DOWN_NOSYNC:
2349 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2350 break;
2351 case F2FS_GOING_DOWN_METAFLUSH:
2352 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2353 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2354 break;
2355 case F2FS_GOING_DOWN_NEED_FSCK:
2356 set_sbi_flag(sbi, SBI_NEED_FSCK);
2357 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2358 set_sbi_flag(sbi, SBI_IS_DIRTY);
2359 /* do checkpoint only */
2360 ret = f2fs_sync_fs(sb, 1);
2361 if (ret == -EIO)
2362 ret = 0;
2363 goto out;
2364 default:
2365 ret = -EINVAL;
2366 goto out;
2367 }
2368
2369 if (readonly)
2370 goto out;
2371
2372 /*
2373 * grab sb->s_umount to avoid racing w/ remount() and other shutdown
2374 * paths.
2375 */
2376 if (need_lock)
2377 down_write(&sbi->sb->s_umount);
2378
2379 f2fs_stop_gc_thread(sbi);
2380 f2fs_stop_discard_thread(sbi);
2381
2382 f2fs_drop_discard_cmd(sbi);
2383 clear_opt(sbi, DISCARD);
2384
2385 if (need_lock)
2386 up_write(&sbi->sb->s_umount);
2387
2388 f2fs_update_time(sbi, REQ_TIME);
2389out:
2390
2391 trace_f2fs_shutdown(sbi, flag, ret);
2392
2393 return ret;
2394}
2395
2396static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2397{
2398 struct inode *inode = file_inode(filp);
2399 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2400 __u32 in;
2401 int ret;
2402 bool need_drop = false, readonly = false;
2403
2404 if (!capable(CAP_SYS_ADMIN))
2405 return -EPERM;
2406
2407 if (get_user(in, (__u32 __user *)arg))
2408 return -EFAULT;
2409
2410 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2411 ret = mnt_want_write_file(filp);
2412 if (ret) {
2413 if (ret != -EROFS)
2414 return ret;
2415
2416 /* fallback to nosync shutdown for readonly fs */
2417 in = F2FS_GOING_DOWN_NOSYNC;
2418 readonly = true;
2419 } else {
2420 need_drop = true;
2421 }
2422 }
2423
2424 ret = f2fs_do_shutdown(sbi, in, readonly, true);
2425
2426 if (need_drop)
2427 mnt_drop_write_file(filp);
2428
2429 return ret;
2430}
2431
2432static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2433{
2434 struct inode *inode = file_inode(filp);
2435 struct super_block *sb = inode->i_sb;
2436 struct fstrim_range range;
2437 int ret;
2438
2439 if (!capable(CAP_SYS_ADMIN))
2440 return -EPERM;
2441
2442 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2443 return -EOPNOTSUPP;
2444
2445 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2446 sizeof(range)))
2447 return -EFAULT;
2448
2449 ret = mnt_want_write_file(filp);
2450 if (ret)
2451 return ret;
2452
2453 range.minlen = max((unsigned int)range.minlen,
2454 bdev_discard_granularity(sb->s_bdev));
2455 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2456 mnt_drop_write_file(filp);
2457 if (ret < 0)
2458 return ret;
2459
2460 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2461 sizeof(range)))
2462 return -EFAULT;
2463 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2464 return 0;
2465}
2466
2467static bool uuid_is_nonzero(__u8 u[16])
2468{
2469 int i;
2470
2471 for (i = 0; i < 16; i++)
2472 if (u[i])
2473 return true;
2474 return false;
2475}
2476
2477static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2478{
2479 struct inode *inode = file_inode(filp);
2480 int ret;
2481
2482 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2483 return -EOPNOTSUPP;
2484
2485 ret = fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2486 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2487 return ret;
2488}
2489
2490static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2491{
2492 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2493 return -EOPNOTSUPP;
2494 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2495}
2496
2497static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2498{
2499 struct inode *inode = file_inode(filp);
2500 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2501 u8 encrypt_pw_salt[16];
2502 int err;
2503
2504 if (!f2fs_sb_has_encrypt(sbi))
2505 return -EOPNOTSUPP;
2506
2507 err = mnt_want_write_file(filp);
2508 if (err)
2509 return err;
2510
2511 f2fs_down_write(&sbi->sb_lock);
2512
2513 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2514 goto got_it;
2515
2516 /* update superblock with uuid */
2517 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2518
2519 err = f2fs_commit_super(sbi, false);
2520 if (err) {
2521 /* undo new data */
2522 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2523 goto out_err;
2524 }
2525got_it:
2526 memcpy(encrypt_pw_salt, sbi->raw_super->encrypt_pw_salt, 16);
2527out_err:
2528 f2fs_up_write(&sbi->sb_lock);
2529 mnt_drop_write_file(filp);
2530
2531 if (!err && copy_to_user((__u8 __user *)arg, encrypt_pw_salt, 16))
2532 err = -EFAULT;
2533
2534 return err;
2535}
2536
2537static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2538 unsigned long arg)
2539{
2540 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2541 return -EOPNOTSUPP;
2542
2543 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2544}
2545
2546static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2547{
2548 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2549 return -EOPNOTSUPP;
2550
2551 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2552}
2553
2554static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2555{
2556 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2557 return -EOPNOTSUPP;
2558
2559 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2560}
2561
2562static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2563 unsigned long arg)
2564{
2565 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2566 return -EOPNOTSUPP;
2567
2568 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2569}
2570
2571static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2572 unsigned long arg)
2573{
2574 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2575 return -EOPNOTSUPP;
2576
2577 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2578}
2579
2580static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2581{
2582 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2583 return -EOPNOTSUPP;
2584
2585 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2586}
2587
2588static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2589{
2590 struct inode *inode = file_inode(filp);
2591 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2592 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
2593 .no_bg_gc = false,
2594 .should_migrate_blocks = false,
2595 .nr_free_secs = 0 };
2596 __u32 sync;
2597 int ret;
2598
2599 if (!capable(CAP_SYS_ADMIN))
2600 return -EPERM;
2601
2602 if (get_user(sync, (__u32 __user *)arg))
2603 return -EFAULT;
2604
2605 if (f2fs_readonly(sbi->sb))
2606 return -EROFS;
2607
2608 ret = mnt_want_write_file(filp);
2609 if (ret)
2610 return ret;
2611
2612 if (!sync) {
2613 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2614 ret = -EBUSY;
2615 goto out;
2616 }
2617 } else {
2618 f2fs_down_write(&sbi->gc_lock);
2619 }
2620
2621 gc_control.init_gc_type = sync ? FG_GC : BG_GC;
2622 gc_control.err_gc_skipped = sync;
2623 stat_inc_gc_call_count(sbi, FOREGROUND);
2624 ret = f2fs_gc(sbi, &gc_control);
2625out:
2626 mnt_drop_write_file(filp);
2627 return ret;
2628}
2629
2630static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2631{
2632 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2633 struct f2fs_gc_control gc_control = {
2634 .init_gc_type = range->sync ? FG_GC : BG_GC,
2635 .no_bg_gc = false,
2636 .should_migrate_blocks = false,
2637 .err_gc_skipped = range->sync,
2638 .nr_free_secs = 0 };
2639 u64 end;
2640 int ret;
2641
2642 if (!capable(CAP_SYS_ADMIN))
2643 return -EPERM;
2644 if (f2fs_readonly(sbi->sb))
2645 return -EROFS;
2646
2647 end = range->start + range->len;
2648 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2649 end >= MAX_BLKADDR(sbi))
2650 return -EINVAL;
2651
2652 ret = mnt_want_write_file(filp);
2653 if (ret)
2654 return ret;
2655
2656do_more:
2657 if (!range->sync) {
2658 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2659 ret = -EBUSY;
2660 goto out;
2661 }
2662 } else {
2663 f2fs_down_write(&sbi->gc_lock);
2664 }
2665
2666 gc_control.victim_segno = GET_SEGNO(sbi, range->start);
2667 stat_inc_gc_call_count(sbi, FOREGROUND);
2668 ret = f2fs_gc(sbi, &gc_control);
2669 if (ret) {
2670 if (ret == -EBUSY)
2671 ret = -EAGAIN;
2672 goto out;
2673 }
2674 range->start += CAP_BLKS_PER_SEC(sbi);
2675 if (range->start <= end)
2676 goto do_more;
2677out:
2678 mnt_drop_write_file(filp);
2679 return ret;
2680}
2681
2682static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2683{
2684 struct f2fs_gc_range range;
2685
2686 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2687 sizeof(range)))
2688 return -EFAULT;
2689 return __f2fs_ioc_gc_range(filp, &range);
2690}
2691
2692static int f2fs_ioc_write_checkpoint(struct file *filp)
2693{
2694 struct inode *inode = file_inode(filp);
2695 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2696 int ret;
2697
2698 if (!capable(CAP_SYS_ADMIN))
2699 return -EPERM;
2700
2701 if (f2fs_readonly(sbi->sb))
2702 return -EROFS;
2703
2704 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2705 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2706 return -EINVAL;
2707 }
2708
2709 ret = mnt_want_write_file(filp);
2710 if (ret)
2711 return ret;
2712
2713 ret = f2fs_sync_fs(sbi->sb, 1);
2714
2715 mnt_drop_write_file(filp);
2716 return ret;
2717}
2718
2719static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2720 struct file *filp,
2721 struct f2fs_defragment *range)
2722{
2723 struct inode *inode = file_inode(filp);
2724 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2725 .m_seg_type = NO_CHECK_TYPE,
2726 .m_may_create = false };
2727 struct extent_info ei = {};
2728 pgoff_t pg_start, pg_end, next_pgofs;
2729 unsigned int total = 0, sec_num;
2730 block_t blk_end = 0;
2731 bool fragmented = false;
2732 int err;
2733
2734 f2fs_balance_fs(sbi, true);
2735
2736 inode_lock(inode);
2737 pg_start = range->start >> PAGE_SHIFT;
2738 pg_end = min_t(pgoff_t,
2739 (range->start + range->len) >> PAGE_SHIFT,
2740 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE));
2741
2742 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED) ||
2743 f2fs_is_atomic_file(inode)) {
2744 err = -EINVAL;
2745 goto unlock_out;
2746 }
2747
2748 /* if in-place-update policy is enabled, don't waste time here */
2749 set_inode_flag(inode, FI_OPU_WRITE);
2750 if (f2fs_should_update_inplace(inode, NULL)) {
2751 err = -EINVAL;
2752 goto out;
2753 }
2754
2755 /* writeback all dirty pages in the range */
2756 err = filemap_write_and_wait_range(inode->i_mapping,
2757 pg_start << PAGE_SHIFT,
2758 (pg_end << PAGE_SHIFT) - 1);
2759 if (err)
2760 goto out;
2761
2762 /*
2763 * lookup mapping info in extent cache, skip defragmenting if physical
2764 * block addresses are continuous.
2765 */
2766 if (f2fs_lookup_read_extent_cache(inode, pg_start, &ei)) {
2767 if ((pgoff_t)ei.fofs + ei.len >= pg_end)
2768 goto out;
2769 }
2770
2771 map.m_lblk = pg_start;
2772 map.m_next_pgofs = &next_pgofs;
2773
2774 /*
2775 * lookup mapping info in dnode page cache, skip defragmenting if all
2776 * physical block addresses are continuous even if there are hole(s)
2777 * in logical blocks.
2778 */
2779 while (map.m_lblk < pg_end) {
2780 map.m_len = pg_end - map.m_lblk;
2781 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2782 if (err)
2783 goto out;
2784
2785 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2786 map.m_lblk = next_pgofs;
2787 continue;
2788 }
2789
2790 if (blk_end && blk_end != map.m_pblk)
2791 fragmented = true;
2792
2793 /* record total count of block that we're going to move */
2794 total += map.m_len;
2795
2796 blk_end = map.m_pblk + map.m_len;
2797
2798 map.m_lblk += map.m_len;
2799 }
2800
2801 if (!fragmented) {
2802 total = 0;
2803 goto out;
2804 }
2805
2806 sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
2807
2808 /*
2809 * make sure there are enough free section for LFS allocation, this can
2810 * avoid defragment running in SSR mode when free section are allocated
2811 * intensively
2812 */
2813 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2814 err = -EAGAIN;
2815 goto out;
2816 }
2817
2818 map.m_lblk = pg_start;
2819 map.m_len = pg_end - pg_start;
2820 total = 0;
2821
2822 while (map.m_lblk < pg_end) {
2823 pgoff_t idx;
2824 int cnt = 0;
2825
2826do_map:
2827 map.m_len = pg_end - map.m_lblk;
2828 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2829 if (err)
2830 goto clear_out;
2831
2832 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2833 map.m_lblk = next_pgofs;
2834 goto check;
2835 }
2836
2837 set_inode_flag(inode, FI_SKIP_WRITES);
2838
2839 idx = map.m_lblk;
2840 while (idx < map.m_lblk + map.m_len &&
2841 cnt < BLKS_PER_SEG(sbi)) {
2842 struct page *page;
2843
2844 page = f2fs_get_lock_data_page(inode, idx, true);
2845 if (IS_ERR(page)) {
2846 err = PTR_ERR(page);
2847 goto clear_out;
2848 }
2849
2850 f2fs_wait_on_page_writeback(page, DATA, true, true);
2851
2852 set_page_dirty(page);
2853 set_page_private_gcing(page);
2854 f2fs_put_page(page, 1);
2855
2856 idx++;
2857 cnt++;
2858 total++;
2859 }
2860
2861 map.m_lblk = idx;
2862check:
2863 if (map.m_lblk < pg_end && cnt < BLKS_PER_SEG(sbi))
2864 goto do_map;
2865
2866 clear_inode_flag(inode, FI_SKIP_WRITES);
2867
2868 err = filemap_fdatawrite(inode->i_mapping);
2869 if (err)
2870 goto out;
2871 }
2872clear_out:
2873 clear_inode_flag(inode, FI_SKIP_WRITES);
2874out:
2875 clear_inode_flag(inode, FI_OPU_WRITE);
2876unlock_out:
2877 inode_unlock(inode);
2878 if (!err)
2879 range->len = (u64)total << PAGE_SHIFT;
2880 return err;
2881}
2882
2883static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2884{
2885 struct inode *inode = file_inode(filp);
2886 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2887 struct f2fs_defragment range;
2888 int err;
2889
2890 if (!capable(CAP_SYS_ADMIN))
2891 return -EPERM;
2892
2893 if (!S_ISREG(inode->i_mode))
2894 return -EINVAL;
2895
2896 if (f2fs_readonly(sbi->sb))
2897 return -EROFS;
2898
2899 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2900 sizeof(range)))
2901 return -EFAULT;
2902
2903 /* verify alignment of offset & size */
2904 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2905 return -EINVAL;
2906
2907 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2908 max_file_blocks(inode)))
2909 return -EINVAL;
2910
2911 err = mnt_want_write_file(filp);
2912 if (err)
2913 return err;
2914
2915 err = f2fs_defragment_range(sbi, filp, &range);
2916 mnt_drop_write_file(filp);
2917
2918 if (range.len)
2919 f2fs_update_time(sbi, REQ_TIME);
2920 if (err < 0)
2921 return err;
2922
2923 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2924 sizeof(range)))
2925 return -EFAULT;
2926
2927 return 0;
2928}
2929
2930static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2931 struct file *file_out, loff_t pos_out, size_t len)
2932{
2933 struct inode *src = file_inode(file_in);
2934 struct inode *dst = file_inode(file_out);
2935 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2936 size_t olen = len, dst_max_i_size = 0;
2937 size_t dst_osize;
2938 int ret;
2939
2940 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2941 src->i_sb != dst->i_sb)
2942 return -EXDEV;
2943
2944 if (unlikely(f2fs_readonly(src->i_sb)))
2945 return -EROFS;
2946
2947 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2948 return -EINVAL;
2949
2950 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2951 return -EOPNOTSUPP;
2952
2953 if (pos_out < 0 || pos_in < 0)
2954 return -EINVAL;
2955
2956 if (src == dst) {
2957 if (pos_in == pos_out)
2958 return 0;
2959 if (pos_out > pos_in && pos_out < pos_in + len)
2960 return -EINVAL;
2961 }
2962
2963 inode_lock(src);
2964 if (src != dst) {
2965 ret = -EBUSY;
2966 if (!inode_trylock(dst))
2967 goto out;
2968 }
2969
2970 if (f2fs_compressed_file(src) || f2fs_compressed_file(dst) ||
2971 f2fs_is_pinned_file(src) || f2fs_is_pinned_file(dst)) {
2972 ret = -EOPNOTSUPP;
2973 goto out_unlock;
2974 }
2975
2976 if (f2fs_is_atomic_file(src) || f2fs_is_atomic_file(dst)) {
2977 ret = -EINVAL;
2978 goto out_unlock;
2979 }
2980
2981 ret = -EINVAL;
2982 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2983 goto out_unlock;
2984 if (len == 0)
2985 olen = len = src->i_size - pos_in;
2986 if (pos_in + len == src->i_size)
2987 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2988 if (len == 0) {
2989 ret = 0;
2990 goto out_unlock;
2991 }
2992
2993 dst_osize = dst->i_size;
2994 if (pos_out + olen > dst->i_size)
2995 dst_max_i_size = pos_out + olen;
2996
2997 /* verify the end result is block aligned */
2998 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2999 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
3000 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
3001 goto out_unlock;
3002
3003 ret = f2fs_convert_inline_inode(src);
3004 if (ret)
3005 goto out_unlock;
3006
3007 ret = f2fs_convert_inline_inode(dst);
3008 if (ret)
3009 goto out_unlock;
3010
3011 /* write out all dirty pages from offset */
3012 ret = filemap_write_and_wait_range(src->i_mapping,
3013 pos_in, pos_in + len);
3014 if (ret)
3015 goto out_unlock;
3016
3017 ret = filemap_write_and_wait_range(dst->i_mapping,
3018 pos_out, pos_out + len);
3019 if (ret)
3020 goto out_unlock;
3021
3022 f2fs_balance_fs(sbi, true);
3023
3024 f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
3025 if (src != dst) {
3026 ret = -EBUSY;
3027 if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
3028 goto out_src;
3029 }
3030
3031 f2fs_lock_op(sbi);
3032 ret = __exchange_data_block(src, dst, F2FS_BYTES_TO_BLK(pos_in),
3033 F2FS_BYTES_TO_BLK(pos_out),
3034 F2FS_BYTES_TO_BLK(len), false);
3035
3036 if (!ret) {
3037 if (dst_max_i_size)
3038 f2fs_i_size_write(dst, dst_max_i_size);
3039 else if (dst_osize != dst->i_size)
3040 f2fs_i_size_write(dst, dst_osize);
3041 }
3042 f2fs_unlock_op(sbi);
3043
3044 if (src != dst)
3045 f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
3046out_src:
3047 f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
3048 if (ret)
3049 goto out_unlock;
3050
3051 inode_set_mtime_to_ts(src, inode_set_ctime_current(src));
3052 f2fs_mark_inode_dirty_sync(src, false);
3053 if (src != dst) {
3054 inode_set_mtime_to_ts(dst, inode_set_ctime_current(dst));
3055 f2fs_mark_inode_dirty_sync(dst, false);
3056 }
3057 f2fs_update_time(sbi, REQ_TIME);
3058
3059out_unlock:
3060 if (src != dst)
3061 inode_unlock(dst);
3062out:
3063 inode_unlock(src);
3064 return ret;
3065}
3066
3067static int __f2fs_ioc_move_range(struct file *filp,
3068 struct f2fs_move_range *range)
3069{
3070 int err;
3071
3072 if (!(filp->f_mode & FMODE_READ) ||
3073 !(filp->f_mode & FMODE_WRITE))
3074 return -EBADF;
3075
3076 CLASS(fd, dst)(range->dst_fd);
3077 if (fd_empty(dst))
3078 return -EBADF;
3079
3080 if (!(fd_file(dst)->f_mode & FMODE_WRITE))
3081 return -EBADF;
3082
3083 err = mnt_want_write_file(filp);
3084 if (err)
3085 return err;
3086
3087 err = f2fs_move_file_range(filp, range->pos_in, fd_file(dst),
3088 range->pos_out, range->len);
3089
3090 mnt_drop_write_file(filp);
3091 return err;
3092}
3093
3094static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
3095{
3096 struct f2fs_move_range range;
3097
3098 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
3099 sizeof(range)))
3100 return -EFAULT;
3101 return __f2fs_ioc_move_range(filp, &range);
3102}
3103
3104static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
3105{
3106 struct inode *inode = file_inode(filp);
3107 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3108 struct sit_info *sm = SIT_I(sbi);
3109 unsigned int start_segno = 0, end_segno = 0;
3110 unsigned int dev_start_segno = 0, dev_end_segno = 0;
3111 struct f2fs_flush_device range;
3112 struct f2fs_gc_control gc_control = {
3113 .init_gc_type = FG_GC,
3114 .should_migrate_blocks = true,
3115 .err_gc_skipped = true,
3116 .nr_free_secs = 0 };
3117 int ret;
3118
3119 if (!capable(CAP_SYS_ADMIN))
3120 return -EPERM;
3121
3122 if (f2fs_readonly(sbi->sb))
3123 return -EROFS;
3124
3125 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
3126 return -EINVAL;
3127
3128 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
3129 sizeof(range)))
3130 return -EFAULT;
3131
3132 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
3133 __is_large_section(sbi)) {
3134 f2fs_warn(sbi, "Can't flush %u in %d for SEGS_PER_SEC %u != 1",
3135 range.dev_num, sbi->s_ndevs, SEGS_PER_SEC(sbi));
3136 return -EINVAL;
3137 }
3138
3139 ret = mnt_want_write_file(filp);
3140 if (ret)
3141 return ret;
3142
3143 if (range.dev_num != 0)
3144 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
3145 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
3146
3147 start_segno = sm->last_victim[FLUSH_DEVICE];
3148 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
3149 start_segno = dev_start_segno;
3150 end_segno = min(start_segno + range.segments, dev_end_segno);
3151
3152 while (start_segno < end_segno) {
3153 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
3154 ret = -EBUSY;
3155 goto out;
3156 }
3157 sm->last_victim[GC_CB] = end_segno + 1;
3158 sm->last_victim[GC_GREEDY] = end_segno + 1;
3159 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
3160
3161 gc_control.victim_segno = start_segno;
3162 stat_inc_gc_call_count(sbi, FOREGROUND);
3163 ret = f2fs_gc(sbi, &gc_control);
3164 if (ret == -EAGAIN)
3165 ret = 0;
3166 else if (ret < 0)
3167 break;
3168 start_segno++;
3169 }
3170out:
3171 mnt_drop_write_file(filp);
3172 return ret;
3173}
3174
3175static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
3176{
3177 struct inode *inode = file_inode(filp);
3178 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
3179
3180 /* Must validate to set it with SQLite behavior in Android. */
3181 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
3182
3183 return put_user(sb_feature, (u32 __user *)arg);
3184}
3185
3186#ifdef CONFIG_QUOTA
3187int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3188{
3189 struct dquot *transfer_to[MAXQUOTAS] = {};
3190 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3191 struct super_block *sb = sbi->sb;
3192 int err;
3193
3194 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3195 if (IS_ERR(transfer_to[PRJQUOTA]))
3196 return PTR_ERR(transfer_to[PRJQUOTA]);
3197
3198 err = __dquot_transfer(inode, transfer_to);
3199 if (err)
3200 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3201 dqput(transfer_to[PRJQUOTA]);
3202 return err;
3203}
3204
3205static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3206{
3207 struct f2fs_inode_info *fi = F2FS_I(inode);
3208 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3209 struct f2fs_inode *ri = NULL;
3210 kprojid_t kprojid;
3211 int err;
3212
3213 if (!f2fs_sb_has_project_quota(sbi)) {
3214 if (projid != F2FS_DEF_PROJID)
3215 return -EOPNOTSUPP;
3216 else
3217 return 0;
3218 }
3219
3220 if (!f2fs_has_extra_attr(inode))
3221 return -EOPNOTSUPP;
3222
3223 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3224
3225 if (projid_eq(kprojid, fi->i_projid))
3226 return 0;
3227
3228 err = -EPERM;
3229 /* Is it quota file? Do not allow user to mess with it */
3230 if (IS_NOQUOTA(inode))
3231 return err;
3232
3233 if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
3234 return -EOVERFLOW;
3235
3236 err = f2fs_dquot_initialize(inode);
3237 if (err)
3238 return err;
3239
3240 f2fs_lock_op(sbi);
3241 err = f2fs_transfer_project_quota(inode, kprojid);
3242 if (err)
3243 goto out_unlock;
3244
3245 fi->i_projid = kprojid;
3246 inode_set_ctime_current(inode);
3247 f2fs_mark_inode_dirty_sync(inode, true);
3248out_unlock:
3249 f2fs_unlock_op(sbi);
3250 return err;
3251}
3252#else
3253int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3254{
3255 return 0;
3256}
3257
3258static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3259{
3260 if (projid != F2FS_DEF_PROJID)
3261 return -EOPNOTSUPP;
3262 return 0;
3263}
3264#endif
3265
3266int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3267{
3268 struct inode *inode = d_inode(dentry);
3269 struct f2fs_inode_info *fi = F2FS_I(inode);
3270 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3271
3272 if (IS_ENCRYPTED(inode))
3273 fsflags |= FS_ENCRYPT_FL;
3274 if (IS_VERITY(inode))
3275 fsflags |= FS_VERITY_FL;
3276 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3277 fsflags |= FS_INLINE_DATA_FL;
3278 if (is_inode_flag_set(inode, FI_PIN_FILE))
3279 fsflags |= FS_NOCOW_FL;
3280
3281 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3282
3283 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3284 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3285
3286 return 0;
3287}
3288
3289int f2fs_fileattr_set(struct mnt_idmap *idmap,
3290 struct dentry *dentry, struct fileattr *fa)
3291{
3292 struct inode *inode = d_inode(dentry);
3293 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3294 u32 iflags;
3295 int err;
3296
3297 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3298 return -EIO;
3299 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3300 return -ENOSPC;
3301 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3302 return -EOPNOTSUPP;
3303 fsflags &= F2FS_SETTABLE_FS_FL;
3304 if (!fa->flags_valid)
3305 mask &= FS_COMMON_FL;
3306
3307 iflags = f2fs_fsflags_to_iflags(fsflags);
3308 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3309 return -EOPNOTSUPP;
3310
3311 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3312 if (!err)
3313 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3314
3315 return err;
3316}
3317
3318int f2fs_pin_file_control(struct inode *inode, bool inc)
3319{
3320 struct f2fs_inode_info *fi = F2FS_I(inode);
3321 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3322
3323 if (IS_DEVICE_ALIASING(inode))
3324 return -EINVAL;
3325
3326 if (fi->i_gc_failures >= sbi->gc_pin_file_threshold) {
3327 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3328 __func__, inode->i_ino, fi->i_gc_failures);
3329 clear_inode_flag(inode, FI_PIN_FILE);
3330 return -EAGAIN;
3331 }
3332
3333 /* Use i_gc_failures for normal file as a risk signal. */
3334 if (inc)
3335 f2fs_i_gc_failures_write(inode, fi->i_gc_failures + 1);
3336
3337 return 0;
3338}
3339
3340static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3341{
3342 struct inode *inode = file_inode(filp);
3343 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3344 __u32 pin;
3345 int ret = 0;
3346
3347 if (get_user(pin, (__u32 __user *)arg))
3348 return -EFAULT;
3349
3350 if (!S_ISREG(inode->i_mode))
3351 return -EINVAL;
3352
3353 if (f2fs_readonly(sbi->sb))
3354 return -EROFS;
3355
3356 if (!pin && IS_DEVICE_ALIASING(inode))
3357 return -EOPNOTSUPP;
3358
3359 ret = mnt_want_write_file(filp);
3360 if (ret)
3361 return ret;
3362
3363 inode_lock(inode);
3364
3365 if (f2fs_is_atomic_file(inode)) {
3366 ret = -EINVAL;
3367 goto out;
3368 }
3369
3370 if (!pin) {
3371 clear_inode_flag(inode, FI_PIN_FILE);
3372 f2fs_i_gc_failures_write(inode, 0);
3373 goto done;
3374 } else if (f2fs_is_pinned_file(inode)) {
3375 goto done;
3376 }
3377
3378 if (F2FS_HAS_BLOCKS(inode)) {
3379 ret = -EFBIG;
3380 goto out;
3381 }
3382
3383 /* Let's allow file pinning on zoned device. */
3384 if (!f2fs_sb_has_blkzoned(sbi) &&
3385 f2fs_should_update_outplace(inode, NULL)) {
3386 ret = -EINVAL;
3387 goto out;
3388 }
3389
3390 if (f2fs_pin_file_control(inode, false)) {
3391 ret = -EAGAIN;
3392 goto out;
3393 }
3394
3395 ret = f2fs_convert_inline_inode(inode);
3396 if (ret)
3397 goto out;
3398
3399 if (!f2fs_disable_compressed_file(inode)) {
3400 ret = -EOPNOTSUPP;
3401 goto out;
3402 }
3403
3404 set_inode_flag(inode, FI_PIN_FILE);
3405 ret = F2FS_I(inode)->i_gc_failures;
3406done:
3407 f2fs_update_time(sbi, REQ_TIME);
3408out:
3409 inode_unlock(inode);
3410 mnt_drop_write_file(filp);
3411 return ret;
3412}
3413
3414static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3415{
3416 struct inode *inode = file_inode(filp);
3417 __u32 pin = 0;
3418
3419 if (is_inode_flag_set(inode, FI_PIN_FILE))
3420 pin = F2FS_I(inode)->i_gc_failures;
3421 return put_user(pin, (u32 __user *)arg);
3422}
3423
3424static int f2fs_ioc_get_dev_alias_file(struct file *filp, unsigned long arg)
3425{
3426 return put_user(IS_DEVICE_ALIASING(file_inode(filp)) ? 1 : 0,
3427 (u32 __user *)arg);
3428}
3429
3430int f2fs_precache_extents(struct inode *inode)
3431{
3432 struct f2fs_inode_info *fi = F2FS_I(inode);
3433 struct f2fs_map_blocks map;
3434 pgoff_t m_next_extent;
3435 loff_t end;
3436 int err;
3437
3438 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3439 return -EOPNOTSUPP;
3440
3441 map.m_lblk = 0;
3442 map.m_pblk = 0;
3443 map.m_next_pgofs = NULL;
3444 map.m_next_extent = &m_next_extent;
3445 map.m_seg_type = NO_CHECK_TYPE;
3446 map.m_may_create = false;
3447 end = F2FS_BLK_ALIGN(i_size_read(inode));
3448
3449 while (map.m_lblk < end) {
3450 map.m_len = end - map.m_lblk;
3451
3452 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3453 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRECACHE);
3454 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3455 if (err || !map.m_len)
3456 return err;
3457
3458 map.m_lblk = m_next_extent;
3459 }
3460
3461 return 0;
3462}
3463
3464static int f2fs_ioc_precache_extents(struct file *filp)
3465{
3466 return f2fs_precache_extents(file_inode(filp));
3467}
3468
3469static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3470{
3471 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3472 __u64 block_count;
3473
3474 if (!capable(CAP_SYS_ADMIN))
3475 return -EPERM;
3476
3477 if (f2fs_readonly(sbi->sb))
3478 return -EROFS;
3479
3480 if (copy_from_user(&block_count, (void __user *)arg,
3481 sizeof(block_count)))
3482 return -EFAULT;
3483
3484 return f2fs_resize_fs(filp, block_count);
3485}
3486
3487static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3488{
3489 struct inode *inode = file_inode(filp);
3490
3491 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3492
3493 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3494 f2fs_warn(F2FS_I_SB(inode),
3495 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem",
3496 inode->i_ino);
3497 return -EOPNOTSUPP;
3498 }
3499
3500 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3501}
3502
3503static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3504{
3505 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3506 return -EOPNOTSUPP;
3507
3508 return fsverity_ioctl_measure(filp, (void __user *)arg);
3509}
3510
3511static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3512{
3513 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3514 return -EOPNOTSUPP;
3515
3516 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3517}
3518
3519static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3520{
3521 struct inode *inode = file_inode(filp);
3522 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3523 char *vbuf;
3524 int count;
3525 int err = 0;
3526
3527 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3528 if (!vbuf)
3529 return -ENOMEM;
3530
3531 f2fs_down_read(&sbi->sb_lock);
3532 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3533 ARRAY_SIZE(sbi->raw_super->volume_name),
3534 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3535 f2fs_up_read(&sbi->sb_lock);
3536
3537 if (copy_to_user((char __user *)arg, vbuf,
3538 min(FSLABEL_MAX, count)))
3539 err = -EFAULT;
3540
3541 kfree(vbuf);
3542 return err;
3543}
3544
3545static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3546{
3547 struct inode *inode = file_inode(filp);
3548 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3549 char *vbuf;
3550 int err = 0;
3551
3552 if (!capable(CAP_SYS_ADMIN))
3553 return -EPERM;
3554
3555 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3556 if (IS_ERR(vbuf))
3557 return PTR_ERR(vbuf);
3558
3559 err = mnt_want_write_file(filp);
3560 if (err)
3561 goto out;
3562
3563 f2fs_down_write(&sbi->sb_lock);
3564
3565 memset(sbi->raw_super->volume_name, 0,
3566 sizeof(sbi->raw_super->volume_name));
3567 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3568 sbi->raw_super->volume_name,
3569 ARRAY_SIZE(sbi->raw_super->volume_name));
3570
3571 err = f2fs_commit_super(sbi, false);
3572
3573 f2fs_up_write(&sbi->sb_lock);
3574
3575 mnt_drop_write_file(filp);
3576out:
3577 kfree(vbuf);
3578 return err;
3579}
3580
3581static int f2fs_get_compress_blocks(struct inode *inode, __u64 *blocks)
3582{
3583 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3584 return -EOPNOTSUPP;
3585
3586 if (!f2fs_compressed_file(inode))
3587 return -EINVAL;
3588
3589 *blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3590
3591 return 0;
3592}
3593
3594static int f2fs_ioc_get_compress_blocks(struct file *filp, unsigned long arg)
3595{
3596 struct inode *inode = file_inode(filp);
3597 __u64 blocks;
3598 int ret;
3599
3600 ret = f2fs_get_compress_blocks(inode, &blocks);
3601 if (ret < 0)
3602 return ret;
3603
3604 return put_user(blocks, (u64 __user *)arg);
3605}
3606
3607static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3608{
3609 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3610 unsigned int released_blocks = 0;
3611 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3612 block_t blkaddr;
3613 int i;
3614
3615 for (i = 0; i < count; i++) {
3616 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3617 dn->ofs_in_node + i);
3618
3619 if (!__is_valid_data_blkaddr(blkaddr))
3620 continue;
3621 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3622 DATA_GENERIC_ENHANCE)))
3623 return -EFSCORRUPTED;
3624 }
3625
3626 while (count) {
3627 int compr_blocks = 0;
3628
3629 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3630 blkaddr = f2fs_data_blkaddr(dn);
3631
3632 if (i == 0) {
3633 if (blkaddr == COMPRESS_ADDR)
3634 continue;
3635 dn->ofs_in_node += cluster_size;
3636 goto next;
3637 }
3638
3639 if (__is_valid_data_blkaddr(blkaddr))
3640 compr_blocks++;
3641
3642 if (blkaddr != NEW_ADDR)
3643 continue;
3644
3645 f2fs_set_data_blkaddr(dn, NULL_ADDR);
3646 }
3647
3648 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3649 dec_valid_block_count(sbi, dn->inode,
3650 cluster_size - compr_blocks);
3651
3652 released_blocks += cluster_size - compr_blocks;
3653next:
3654 count -= cluster_size;
3655 }
3656
3657 return released_blocks;
3658}
3659
3660static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3661{
3662 struct inode *inode = file_inode(filp);
3663 struct f2fs_inode_info *fi = F2FS_I(inode);
3664 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3665 pgoff_t page_idx = 0, last_idx;
3666 unsigned int released_blocks = 0;
3667 int ret;
3668 int writecount;
3669
3670 if (!f2fs_sb_has_compression(sbi))
3671 return -EOPNOTSUPP;
3672
3673 if (f2fs_readonly(sbi->sb))
3674 return -EROFS;
3675
3676 ret = mnt_want_write_file(filp);
3677 if (ret)
3678 return ret;
3679
3680 f2fs_balance_fs(sbi, true);
3681
3682 inode_lock(inode);
3683
3684 writecount = atomic_read(&inode->i_writecount);
3685 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3686 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3687 ret = -EBUSY;
3688 goto out;
3689 }
3690
3691 if (!f2fs_compressed_file(inode) ||
3692 is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3693 ret = -EINVAL;
3694 goto out;
3695 }
3696
3697 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3698 if (ret)
3699 goto out;
3700
3701 if (!atomic_read(&fi->i_compr_blocks)) {
3702 ret = -EPERM;
3703 goto out;
3704 }
3705
3706 set_inode_flag(inode, FI_COMPRESS_RELEASED);
3707 inode_set_ctime_current(inode);
3708 f2fs_mark_inode_dirty_sync(inode, true);
3709
3710 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3711 filemap_invalidate_lock(inode->i_mapping);
3712
3713 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3714
3715 while (page_idx < last_idx) {
3716 struct dnode_of_data dn;
3717 pgoff_t end_offset, count;
3718
3719 f2fs_lock_op(sbi);
3720
3721 set_new_dnode(&dn, inode, NULL, NULL, 0);
3722 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3723 if (ret) {
3724 f2fs_unlock_op(sbi);
3725 if (ret == -ENOENT) {
3726 page_idx = f2fs_get_next_page_offset(&dn,
3727 page_idx);
3728 ret = 0;
3729 continue;
3730 }
3731 break;
3732 }
3733
3734 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3735 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3736 count = round_up(count, fi->i_cluster_size);
3737
3738 ret = release_compress_blocks(&dn, count);
3739
3740 f2fs_put_dnode(&dn);
3741
3742 f2fs_unlock_op(sbi);
3743
3744 if (ret < 0)
3745 break;
3746
3747 page_idx += count;
3748 released_blocks += ret;
3749 }
3750
3751 filemap_invalidate_unlock(inode->i_mapping);
3752 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3753out:
3754 if (released_blocks)
3755 f2fs_update_time(sbi, REQ_TIME);
3756 inode_unlock(inode);
3757
3758 mnt_drop_write_file(filp);
3759
3760 if (ret >= 0) {
3761 ret = put_user(released_blocks, (u64 __user *)arg);
3762 } else if (released_blocks &&
3763 atomic_read(&fi->i_compr_blocks)) {
3764 set_sbi_flag(sbi, SBI_NEED_FSCK);
3765 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3766 "iblocks=%llu, released=%u, compr_blocks=%u, "
3767 "run fsck to fix.",
3768 __func__, inode->i_ino, inode->i_blocks,
3769 released_blocks,
3770 atomic_read(&fi->i_compr_blocks));
3771 }
3772
3773 return ret;
3774}
3775
3776static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count,
3777 unsigned int *reserved_blocks)
3778{
3779 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3780 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3781 block_t blkaddr;
3782 int i;
3783
3784 for (i = 0; i < count; i++) {
3785 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3786 dn->ofs_in_node + i);
3787
3788 if (!__is_valid_data_blkaddr(blkaddr))
3789 continue;
3790 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3791 DATA_GENERIC_ENHANCE)))
3792 return -EFSCORRUPTED;
3793 }
3794
3795 while (count) {
3796 int compr_blocks = 0;
3797 blkcnt_t reserved = 0;
3798 blkcnt_t to_reserved;
3799 int ret;
3800
3801 for (i = 0; i < cluster_size; i++) {
3802 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3803 dn->ofs_in_node + i);
3804
3805 if (i == 0) {
3806 if (blkaddr != COMPRESS_ADDR) {
3807 dn->ofs_in_node += cluster_size;
3808 goto next;
3809 }
3810 continue;
3811 }
3812
3813 /*
3814 * compressed cluster was not released due to it
3815 * fails in release_compress_blocks(), so NEW_ADDR
3816 * is a possible case.
3817 */
3818 if (blkaddr == NEW_ADDR) {
3819 reserved++;
3820 continue;
3821 }
3822 if (__is_valid_data_blkaddr(blkaddr)) {
3823 compr_blocks++;
3824 continue;
3825 }
3826 }
3827
3828 to_reserved = cluster_size - compr_blocks - reserved;
3829
3830 /* for the case all blocks in cluster were reserved */
3831 if (reserved && to_reserved == 1) {
3832 dn->ofs_in_node += cluster_size;
3833 goto next;
3834 }
3835
3836 ret = inc_valid_block_count(sbi, dn->inode,
3837 &to_reserved, false);
3838 if (unlikely(ret))
3839 return ret;
3840
3841 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3842 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
3843 f2fs_set_data_blkaddr(dn, NEW_ADDR);
3844 }
3845
3846 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3847
3848 *reserved_blocks += to_reserved;
3849next:
3850 count -= cluster_size;
3851 }
3852
3853 return 0;
3854}
3855
3856static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3857{
3858 struct inode *inode = file_inode(filp);
3859 struct f2fs_inode_info *fi = F2FS_I(inode);
3860 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3861 pgoff_t page_idx = 0, last_idx;
3862 unsigned int reserved_blocks = 0;
3863 int ret;
3864
3865 if (!f2fs_sb_has_compression(sbi))
3866 return -EOPNOTSUPP;
3867
3868 if (f2fs_readonly(sbi->sb))
3869 return -EROFS;
3870
3871 ret = mnt_want_write_file(filp);
3872 if (ret)
3873 return ret;
3874
3875 f2fs_balance_fs(sbi, true);
3876
3877 inode_lock(inode);
3878
3879 if (!f2fs_compressed_file(inode) ||
3880 !is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3881 ret = -EINVAL;
3882 goto unlock_inode;
3883 }
3884
3885 if (atomic_read(&fi->i_compr_blocks))
3886 goto unlock_inode;
3887
3888 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3889 filemap_invalidate_lock(inode->i_mapping);
3890
3891 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3892
3893 while (page_idx < last_idx) {
3894 struct dnode_of_data dn;
3895 pgoff_t end_offset, count;
3896
3897 f2fs_lock_op(sbi);
3898
3899 set_new_dnode(&dn, inode, NULL, NULL, 0);
3900 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3901 if (ret) {
3902 f2fs_unlock_op(sbi);
3903 if (ret == -ENOENT) {
3904 page_idx = f2fs_get_next_page_offset(&dn,
3905 page_idx);
3906 ret = 0;
3907 continue;
3908 }
3909 break;
3910 }
3911
3912 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3913 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3914 count = round_up(count, fi->i_cluster_size);
3915
3916 ret = reserve_compress_blocks(&dn, count, &reserved_blocks);
3917
3918 f2fs_put_dnode(&dn);
3919
3920 f2fs_unlock_op(sbi);
3921
3922 if (ret < 0)
3923 break;
3924
3925 page_idx += count;
3926 }
3927
3928 filemap_invalidate_unlock(inode->i_mapping);
3929 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3930
3931 if (!ret) {
3932 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
3933 inode_set_ctime_current(inode);
3934 f2fs_mark_inode_dirty_sync(inode, true);
3935 }
3936unlock_inode:
3937 if (reserved_blocks)
3938 f2fs_update_time(sbi, REQ_TIME);
3939 inode_unlock(inode);
3940 mnt_drop_write_file(filp);
3941
3942 if (!ret) {
3943 ret = put_user(reserved_blocks, (u64 __user *)arg);
3944 } else if (reserved_blocks &&
3945 atomic_read(&fi->i_compr_blocks)) {
3946 set_sbi_flag(sbi, SBI_NEED_FSCK);
3947 f2fs_warn(sbi, "%s: partial blocks were reserved i_ino=%lx "
3948 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3949 "run fsck to fix.",
3950 __func__, inode->i_ino, inode->i_blocks,
3951 reserved_blocks,
3952 atomic_read(&fi->i_compr_blocks));
3953 }
3954
3955 return ret;
3956}
3957
3958static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3959 pgoff_t off, block_t block, block_t len, u32 flags)
3960{
3961 sector_t sector = SECTOR_FROM_BLOCK(block);
3962 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3963 int ret = 0;
3964
3965 if (flags & F2FS_TRIM_FILE_DISCARD) {
3966 if (bdev_max_secure_erase_sectors(bdev))
3967 ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
3968 GFP_NOFS);
3969 else
3970 ret = blkdev_issue_discard(bdev, sector, nr_sects,
3971 GFP_NOFS);
3972 }
3973
3974 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3975 if (IS_ENCRYPTED(inode))
3976 ret = fscrypt_zeroout_range(inode, off, block, len);
3977 else
3978 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3979 GFP_NOFS, 0);
3980 }
3981
3982 return ret;
3983}
3984
3985static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3986{
3987 struct inode *inode = file_inode(filp);
3988 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3989 struct address_space *mapping = inode->i_mapping;
3990 struct block_device *prev_bdev = NULL;
3991 struct f2fs_sectrim_range range;
3992 pgoff_t index, pg_end, prev_index = 0;
3993 block_t prev_block = 0, len = 0;
3994 loff_t end_addr;
3995 bool to_end = false;
3996 int ret = 0;
3997
3998 if (!(filp->f_mode & FMODE_WRITE))
3999 return -EBADF;
4000
4001 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
4002 sizeof(range)))
4003 return -EFAULT;
4004
4005 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
4006 !S_ISREG(inode->i_mode))
4007 return -EINVAL;
4008
4009 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
4010 !f2fs_hw_support_discard(sbi)) ||
4011 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
4012 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
4013 return -EOPNOTSUPP;
4014
4015 ret = mnt_want_write_file(filp);
4016 if (ret)
4017 return ret;
4018 inode_lock(inode);
4019
4020 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
4021 range.start >= inode->i_size) {
4022 ret = -EINVAL;
4023 goto err;
4024 }
4025
4026 if (range.len == 0)
4027 goto err;
4028
4029 if (inode->i_size - range.start > range.len) {
4030 end_addr = range.start + range.len;
4031 } else {
4032 end_addr = range.len == (u64)-1 ?
4033 sbi->sb->s_maxbytes : inode->i_size;
4034 to_end = true;
4035 }
4036
4037 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
4038 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
4039 ret = -EINVAL;
4040 goto err;
4041 }
4042
4043 index = F2FS_BYTES_TO_BLK(range.start);
4044 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
4045
4046 ret = f2fs_convert_inline_inode(inode);
4047 if (ret)
4048 goto err;
4049
4050 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4051 filemap_invalidate_lock(mapping);
4052
4053 ret = filemap_write_and_wait_range(mapping, range.start,
4054 to_end ? LLONG_MAX : end_addr - 1);
4055 if (ret)
4056 goto out;
4057
4058 truncate_inode_pages_range(mapping, range.start,
4059 to_end ? -1 : end_addr - 1);
4060
4061 while (index < pg_end) {
4062 struct dnode_of_data dn;
4063 pgoff_t end_offset, count;
4064 int i;
4065
4066 set_new_dnode(&dn, inode, NULL, NULL, 0);
4067 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
4068 if (ret) {
4069 if (ret == -ENOENT) {
4070 index = f2fs_get_next_page_offset(&dn, index);
4071 continue;
4072 }
4073 goto out;
4074 }
4075
4076 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
4077 count = min(end_offset - dn.ofs_in_node, pg_end - index);
4078 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
4079 struct block_device *cur_bdev;
4080 block_t blkaddr = f2fs_data_blkaddr(&dn);
4081
4082 if (!__is_valid_data_blkaddr(blkaddr))
4083 continue;
4084
4085 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
4086 DATA_GENERIC_ENHANCE)) {
4087 ret = -EFSCORRUPTED;
4088 f2fs_put_dnode(&dn);
4089 goto out;
4090 }
4091
4092 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
4093 if (f2fs_is_multi_device(sbi)) {
4094 int di = f2fs_target_device_index(sbi, blkaddr);
4095
4096 blkaddr -= FDEV(di).start_blk;
4097 }
4098
4099 if (len) {
4100 if (prev_bdev == cur_bdev &&
4101 index == prev_index + len &&
4102 blkaddr == prev_block + len) {
4103 len++;
4104 } else {
4105 ret = f2fs_secure_erase(prev_bdev,
4106 inode, prev_index, prev_block,
4107 len, range.flags);
4108 if (ret) {
4109 f2fs_put_dnode(&dn);
4110 goto out;
4111 }
4112
4113 len = 0;
4114 }
4115 }
4116
4117 if (!len) {
4118 prev_bdev = cur_bdev;
4119 prev_index = index;
4120 prev_block = blkaddr;
4121 len = 1;
4122 }
4123 }
4124
4125 f2fs_put_dnode(&dn);
4126
4127 if (fatal_signal_pending(current)) {
4128 ret = -EINTR;
4129 goto out;
4130 }
4131 cond_resched();
4132 }
4133
4134 if (len)
4135 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
4136 prev_block, len, range.flags);
4137 f2fs_update_time(sbi, REQ_TIME);
4138out:
4139 filemap_invalidate_unlock(mapping);
4140 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4141err:
4142 inode_unlock(inode);
4143 mnt_drop_write_file(filp);
4144
4145 return ret;
4146}
4147
4148static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
4149{
4150 struct inode *inode = file_inode(filp);
4151 struct f2fs_comp_option option;
4152
4153 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
4154 return -EOPNOTSUPP;
4155
4156 inode_lock_shared(inode);
4157
4158 if (!f2fs_compressed_file(inode)) {
4159 inode_unlock_shared(inode);
4160 return -ENODATA;
4161 }
4162
4163 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
4164 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
4165
4166 inode_unlock_shared(inode);
4167
4168 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
4169 sizeof(option)))
4170 return -EFAULT;
4171
4172 return 0;
4173}
4174
4175static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
4176{
4177 struct inode *inode = file_inode(filp);
4178 struct f2fs_inode_info *fi = F2FS_I(inode);
4179 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4180 struct f2fs_comp_option option;
4181 int ret = 0;
4182
4183 if (!f2fs_sb_has_compression(sbi))
4184 return -EOPNOTSUPP;
4185
4186 if (!(filp->f_mode & FMODE_WRITE))
4187 return -EBADF;
4188
4189 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
4190 sizeof(option)))
4191 return -EFAULT;
4192
4193 if (option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
4194 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
4195 option.algorithm >= COMPRESS_MAX)
4196 return -EINVAL;
4197
4198 ret = mnt_want_write_file(filp);
4199 if (ret)
4200 return ret;
4201 inode_lock(inode);
4202
4203 f2fs_down_write(&F2FS_I(inode)->i_sem);
4204 if (!f2fs_compressed_file(inode)) {
4205 ret = -EINVAL;
4206 goto out;
4207 }
4208
4209 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
4210 ret = -EBUSY;
4211 goto out;
4212 }
4213
4214 if (F2FS_HAS_BLOCKS(inode)) {
4215 ret = -EFBIG;
4216 goto out;
4217 }
4218
4219 fi->i_compress_algorithm = option.algorithm;
4220 fi->i_log_cluster_size = option.log_cluster_size;
4221 fi->i_cluster_size = BIT(option.log_cluster_size);
4222 /* Set default level */
4223 if (fi->i_compress_algorithm == COMPRESS_ZSTD)
4224 fi->i_compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
4225 else
4226 fi->i_compress_level = 0;
4227 /* Adjust mount option level */
4228 if (option.algorithm == F2FS_OPTION(sbi).compress_algorithm &&
4229 F2FS_OPTION(sbi).compress_level)
4230 fi->i_compress_level = F2FS_OPTION(sbi).compress_level;
4231 f2fs_mark_inode_dirty_sync(inode, true);
4232
4233 if (!f2fs_is_compress_backend_ready(inode))
4234 f2fs_warn(sbi, "compression algorithm is successfully set, "
4235 "but current kernel doesn't support this algorithm.");
4236out:
4237 f2fs_up_write(&fi->i_sem);
4238 inode_unlock(inode);
4239 mnt_drop_write_file(filp);
4240
4241 return ret;
4242}
4243
4244static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
4245{
4246 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
4247 struct address_space *mapping = inode->i_mapping;
4248 struct page *page;
4249 pgoff_t redirty_idx = page_idx;
4250 int i, page_len = 0, ret = 0;
4251
4252 page_cache_ra_unbounded(&ractl, len, 0);
4253
4254 for (i = 0; i < len; i++, page_idx++) {
4255 page = read_cache_page(mapping, page_idx, NULL, NULL);
4256 if (IS_ERR(page)) {
4257 ret = PTR_ERR(page);
4258 break;
4259 }
4260 page_len++;
4261 }
4262
4263 for (i = 0; i < page_len; i++, redirty_idx++) {
4264 page = find_lock_page(mapping, redirty_idx);
4265
4266 /* It will never fail, when page has pinned above */
4267 f2fs_bug_on(F2FS_I_SB(inode), !page);
4268
4269 f2fs_wait_on_page_writeback(page, DATA, true, true);
4270
4271 set_page_dirty(page);
4272 set_page_private_gcing(page);
4273 f2fs_put_page(page, 1);
4274 f2fs_put_page(page, 0);
4275 }
4276
4277 return ret;
4278}
4279
4280static int f2fs_ioc_decompress_file(struct file *filp)
4281{
4282 struct inode *inode = file_inode(filp);
4283 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4284 struct f2fs_inode_info *fi = F2FS_I(inode);
4285 pgoff_t page_idx = 0, last_idx, cluster_idx;
4286 int ret;
4287
4288 if (!f2fs_sb_has_compression(sbi) ||
4289 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4290 return -EOPNOTSUPP;
4291
4292 if (!(filp->f_mode & FMODE_WRITE))
4293 return -EBADF;
4294
4295 f2fs_balance_fs(sbi, true);
4296
4297 ret = mnt_want_write_file(filp);
4298 if (ret)
4299 return ret;
4300 inode_lock(inode);
4301
4302 if (!f2fs_is_compress_backend_ready(inode)) {
4303 ret = -EOPNOTSUPP;
4304 goto out;
4305 }
4306
4307 if (!f2fs_compressed_file(inode) ||
4308 is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4309 ret = -EINVAL;
4310 goto out;
4311 }
4312
4313 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4314 if (ret)
4315 goto out;
4316
4317 if (!atomic_read(&fi->i_compr_blocks))
4318 goto out;
4319
4320 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4321 last_idx >>= fi->i_log_cluster_size;
4322
4323 for (cluster_idx = 0; cluster_idx < last_idx; cluster_idx++) {
4324 page_idx = cluster_idx << fi->i_log_cluster_size;
4325
4326 if (!f2fs_is_compressed_cluster(inode, page_idx))
4327 continue;
4328
4329 ret = redirty_blocks(inode, page_idx, fi->i_cluster_size);
4330 if (ret < 0)
4331 break;
4332
4333 if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
4334 ret = filemap_fdatawrite(inode->i_mapping);
4335 if (ret < 0)
4336 break;
4337 }
4338
4339 cond_resched();
4340 if (fatal_signal_pending(current)) {
4341 ret = -EINTR;
4342 break;
4343 }
4344 }
4345
4346 if (!ret)
4347 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4348 LLONG_MAX);
4349
4350 if (ret)
4351 f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
4352 __func__, ret);
4353 f2fs_update_time(sbi, REQ_TIME);
4354out:
4355 inode_unlock(inode);
4356 mnt_drop_write_file(filp);
4357
4358 return ret;
4359}
4360
4361static int f2fs_ioc_compress_file(struct file *filp)
4362{
4363 struct inode *inode = file_inode(filp);
4364 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4365 struct f2fs_inode_info *fi = F2FS_I(inode);
4366 pgoff_t page_idx = 0, last_idx, cluster_idx;
4367 int ret;
4368
4369 if (!f2fs_sb_has_compression(sbi) ||
4370 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4371 return -EOPNOTSUPP;
4372
4373 if (!(filp->f_mode & FMODE_WRITE))
4374 return -EBADF;
4375
4376 f2fs_balance_fs(sbi, true);
4377
4378 ret = mnt_want_write_file(filp);
4379 if (ret)
4380 return ret;
4381 inode_lock(inode);
4382
4383 if (!f2fs_is_compress_backend_ready(inode)) {
4384 ret = -EOPNOTSUPP;
4385 goto out;
4386 }
4387
4388 if (!f2fs_compressed_file(inode) ||
4389 is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4390 ret = -EINVAL;
4391 goto out;
4392 }
4393
4394 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4395 if (ret)
4396 goto out;
4397
4398 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4399
4400 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4401 last_idx >>= fi->i_log_cluster_size;
4402
4403 for (cluster_idx = 0; cluster_idx < last_idx; cluster_idx++) {
4404 page_idx = cluster_idx << fi->i_log_cluster_size;
4405
4406 if (f2fs_is_sparse_cluster(inode, page_idx))
4407 continue;
4408
4409 ret = redirty_blocks(inode, page_idx, fi->i_cluster_size);
4410 if (ret < 0)
4411 break;
4412
4413 if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
4414 ret = filemap_fdatawrite(inode->i_mapping);
4415 if (ret < 0)
4416 break;
4417 }
4418
4419 cond_resched();
4420 if (fatal_signal_pending(current)) {
4421 ret = -EINTR;
4422 break;
4423 }
4424 }
4425
4426 if (!ret)
4427 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4428 LLONG_MAX);
4429
4430 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4431
4432 if (ret)
4433 f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
4434 __func__, ret);
4435 f2fs_update_time(sbi, REQ_TIME);
4436out:
4437 inode_unlock(inode);
4438 mnt_drop_write_file(filp);
4439
4440 return ret;
4441}
4442
4443static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4444{
4445 switch (cmd) {
4446 case FS_IOC_GETVERSION:
4447 return f2fs_ioc_getversion(filp, arg);
4448 case F2FS_IOC_START_ATOMIC_WRITE:
4449 return f2fs_ioc_start_atomic_write(filp, false);
4450 case F2FS_IOC_START_ATOMIC_REPLACE:
4451 return f2fs_ioc_start_atomic_write(filp, true);
4452 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4453 return f2fs_ioc_commit_atomic_write(filp);
4454 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4455 return f2fs_ioc_abort_atomic_write(filp);
4456 case F2FS_IOC_START_VOLATILE_WRITE:
4457 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4458 return -EOPNOTSUPP;
4459 case F2FS_IOC_SHUTDOWN:
4460 return f2fs_ioc_shutdown(filp, arg);
4461 case FITRIM:
4462 return f2fs_ioc_fitrim(filp, arg);
4463 case FS_IOC_SET_ENCRYPTION_POLICY:
4464 return f2fs_ioc_set_encryption_policy(filp, arg);
4465 case FS_IOC_GET_ENCRYPTION_POLICY:
4466 return f2fs_ioc_get_encryption_policy(filp, arg);
4467 case FS_IOC_GET_ENCRYPTION_PWSALT:
4468 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4469 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4470 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4471 case FS_IOC_ADD_ENCRYPTION_KEY:
4472 return f2fs_ioc_add_encryption_key(filp, arg);
4473 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4474 return f2fs_ioc_remove_encryption_key(filp, arg);
4475 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4476 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4477 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4478 return f2fs_ioc_get_encryption_key_status(filp, arg);
4479 case FS_IOC_GET_ENCRYPTION_NONCE:
4480 return f2fs_ioc_get_encryption_nonce(filp, arg);
4481 case F2FS_IOC_GARBAGE_COLLECT:
4482 return f2fs_ioc_gc(filp, arg);
4483 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4484 return f2fs_ioc_gc_range(filp, arg);
4485 case F2FS_IOC_WRITE_CHECKPOINT:
4486 return f2fs_ioc_write_checkpoint(filp);
4487 case F2FS_IOC_DEFRAGMENT:
4488 return f2fs_ioc_defragment(filp, arg);
4489 case F2FS_IOC_MOVE_RANGE:
4490 return f2fs_ioc_move_range(filp, arg);
4491 case F2FS_IOC_FLUSH_DEVICE:
4492 return f2fs_ioc_flush_device(filp, arg);
4493 case F2FS_IOC_GET_FEATURES:
4494 return f2fs_ioc_get_features(filp, arg);
4495 case F2FS_IOC_GET_PIN_FILE:
4496 return f2fs_ioc_get_pin_file(filp, arg);
4497 case F2FS_IOC_SET_PIN_FILE:
4498 return f2fs_ioc_set_pin_file(filp, arg);
4499 case F2FS_IOC_PRECACHE_EXTENTS:
4500 return f2fs_ioc_precache_extents(filp);
4501 case F2FS_IOC_RESIZE_FS:
4502 return f2fs_ioc_resize_fs(filp, arg);
4503 case FS_IOC_ENABLE_VERITY:
4504 return f2fs_ioc_enable_verity(filp, arg);
4505 case FS_IOC_MEASURE_VERITY:
4506 return f2fs_ioc_measure_verity(filp, arg);
4507 case FS_IOC_READ_VERITY_METADATA:
4508 return f2fs_ioc_read_verity_metadata(filp, arg);
4509 case FS_IOC_GETFSLABEL:
4510 return f2fs_ioc_getfslabel(filp, arg);
4511 case FS_IOC_SETFSLABEL:
4512 return f2fs_ioc_setfslabel(filp, arg);
4513 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4514 return f2fs_ioc_get_compress_blocks(filp, arg);
4515 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4516 return f2fs_release_compress_blocks(filp, arg);
4517 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4518 return f2fs_reserve_compress_blocks(filp, arg);
4519 case F2FS_IOC_SEC_TRIM_FILE:
4520 return f2fs_sec_trim_file(filp, arg);
4521 case F2FS_IOC_GET_COMPRESS_OPTION:
4522 return f2fs_ioc_get_compress_option(filp, arg);
4523 case F2FS_IOC_SET_COMPRESS_OPTION:
4524 return f2fs_ioc_set_compress_option(filp, arg);
4525 case F2FS_IOC_DECOMPRESS_FILE:
4526 return f2fs_ioc_decompress_file(filp);
4527 case F2FS_IOC_COMPRESS_FILE:
4528 return f2fs_ioc_compress_file(filp);
4529 case F2FS_IOC_GET_DEV_ALIAS_FILE:
4530 return f2fs_ioc_get_dev_alias_file(filp, arg);
4531 default:
4532 return -ENOTTY;
4533 }
4534}
4535
4536long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4537{
4538 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4539 return -EIO;
4540 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4541 return -ENOSPC;
4542
4543 return __f2fs_ioctl(filp, cmd, arg);
4544}
4545
4546/*
4547 * Return %true if the given read or write request should use direct I/O, or
4548 * %false if it should use buffered I/O.
4549 */
4550static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
4551 struct iov_iter *iter)
4552{
4553 unsigned int align;
4554
4555 if (!(iocb->ki_flags & IOCB_DIRECT))
4556 return false;
4557
4558 if (f2fs_force_buffered_io(inode, iov_iter_rw(iter)))
4559 return false;
4560
4561 /*
4562 * Direct I/O not aligned to the disk's logical_block_size will be
4563 * attempted, but will fail with -EINVAL.
4564 *
4565 * f2fs additionally requires that direct I/O be aligned to the
4566 * filesystem block size, which is often a stricter requirement.
4567 * However, f2fs traditionally falls back to buffered I/O on requests
4568 * that are logical_block_size-aligned but not fs-block aligned.
4569 *
4570 * The below logic implements this behavior.
4571 */
4572 align = iocb->ki_pos | iov_iter_alignment(iter);
4573 if (!IS_ALIGNED(align, i_blocksize(inode)) &&
4574 IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
4575 return false;
4576
4577 return true;
4578}
4579
4580static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
4581 unsigned int flags)
4582{
4583 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4584
4585 dec_page_count(sbi, F2FS_DIO_READ);
4586 if (error)
4587 return error;
4588 f2fs_update_iostat(sbi, NULL, APP_DIRECT_READ_IO, size);
4589 return 0;
4590}
4591
4592static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
4593 .end_io = f2fs_dio_read_end_io,
4594};
4595
4596static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
4597{
4598 struct file *file = iocb->ki_filp;
4599 struct inode *inode = file_inode(file);
4600 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4601 struct f2fs_inode_info *fi = F2FS_I(inode);
4602 const loff_t pos = iocb->ki_pos;
4603 const size_t count = iov_iter_count(to);
4604 struct iomap_dio *dio;
4605 ssize_t ret;
4606
4607 if (count == 0)
4608 return 0; /* skip atime update */
4609
4610 trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
4611
4612 if (iocb->ki_flags & IOCB_NOWAIT) {
4613 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4614 ret = -EAGAIN;
4615 goto out;
4616 }
4617 } else {
4618 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4619 }
4620
4621 /* dio is not compatible w/ atomic file */
4622 if (f2fs_is_atomic_file(inode)) {
4623 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4624 ret = -EOPNOTSUPP;
4625 goto out;
4626 }
4627
4628 /*
4629 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4630 * the higher-level function iomap_dio_rw() in order to ensure that the
4631 * F2FS_DIO_READ counter will be decremented correctly in all cases.
4632 */
4633 inc_page_count(sbi, F2FS_DIO_READ);
4634 dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
4635 &f2fs_iomap_dio_read_ops, 0, NULL, 0);
4636 if (IS_ERR_OR_NULL(dio)) {
4637 ret = PTR_ERR_OR_ZERO(dio);
4638 if (ret != -EIOCBQUEUED)
4639 dec_page_count(sbi, F2FS_DIO_READ);
4640 } else {
4641 ret = iomap_dio_complete(dio);
4642 }
4643
4644 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4645
4646 file_accessed(file);
4647out:
4648 trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
4649 return ret;
4650}
4651
4652static void f2fs_trace_rw_file_path(struct file *file, loff_t pos, size_t count,
4653 int rw)
4654{
4655 struct inode *inode = file_inode(file);
4656 char *buf, *path;
4657
4658 buf = f2fs_getname(F2FS_I_SB(inode));
4659 if (!buf)
4660 return;
4661 path = dentry_path_raw(file_dentry(file), buf, PATH_MAX);
4662 if (IS_ERR(path))
4663 goto free_buf;
4664 if (rw == WRITE)
4665 trace_f2fs_datawrite_start(inode, pos, count,
4666 current->pid, path, current->comm);
4667 else
4668 trace_f2fs_dataread_start(inode, pos, count,
4669 current->pid, path, current->comm);
4670free_buf:
4671 f2fs_putname(buf);
4672}
4673
4674static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
4675{
4676 struct inode *inode = file_inode(iocb->ki_filp);
4677 const loff_t pos = iocb->ki_pos;
4678 ssize_t ret;
4679
4680 if (!f2fs_is_compress_backend_ready(inode))
4681 return -EOPNOTSUPP;
4682
4683 if (trace_f2fs_dataread_start_enabled())
4684 f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
4685 iov_iter_count(to), READ);
4686
4687 /* In LFS mode, if there is inflight dio, wait for its completion */
4688 if (f2fs_lfs_mode(F2FS_I_SB(inode)) &&
4689 get_pages(F2FS_I_SB(inode), F2FS_DIO_WRITE))
4690 inode_dio_wait(inode);
4691
4692 if (f2fs_should_use_dio(inode, iocb, to)) {
4693 ret = f2fs_dio_read_iter(iocb, to);
4694 } else {
4695 ret = filemap_read(iocb, to, 0);
4696 if (ret > 0)
4697 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4698 APP_BUFFERED_READ_IO, ret);
4699 }
4700 if (trace_f2fs_dataread_end_enabled())
4701 trace_f2fs_dataread_end(inode, pos, ret);
4702 return ret;
4703}
4704
4705static ssize_t f2fs_file_splice_read(struct file *in, loff_t *ppos,
4706 struct pipe_inode_info *pipe,
4707 size_t len, unsigned int flags)
4708{
4709 struct inode *inode = file_inode(in);
4710 const loff_t pos = *ppos;
4711 ssize_t ret;
4712
4713 if (!f2fs_is_compress_backend_ready(inode))
4714 return -EOPNOTSUPP;
4715
4716 if (trace_f2fs_dataread_start_enabled())
4717 f2fs_trace_rw_file_path(in, pos, len, READ);
4718
4719 ret = filemap_splice_read(in, ppos, pipe, len, flags);
4720 if (ret > 0)
4721 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4722 APP_BUFFERED_READ_IO, ret);
4723
4724 if (trace_f2fs_dataread_end_enabled())
4725 trace_f2fs_dataread_end(inode, pos, ret);
4726 return ret;
4727}
4728
4729static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
4730{
4731 struct file *file = iocb->ki_filp;
4732 struct inode *inode = file_inode(file);
4733 ssize_t count;
4734 int err;
4735
4736 if (IS_IMMUTABLE(inode))
4737 return -EPERM;
4738
4739 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
4740 return -EPERM;
4741
4742 count = generic_write_checks(iocb, from);
4743 if (count <= 0)
4744 return count;
4745
4746 err = file_modified(file);
4747 if (err)
4748 return err;
4749 return count;
4750}
4751
4752/*
4753 * Preallocate blocks for a write request, if it is possible and helpful to do
4754 * so. Returns a positive number if blocks may have been preallocated, 0 if no
4755 * blocks were preallocated, or a negative errno value if something went
4756 * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
4757 * requested blocks (not just some of them) have been allocated.
4758 */
4759static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
4760 bool dio)
4761{
4762 struct inode *inode = file_inode(iocb->ki_filp);
4763 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4764 const loff_t pos = iocb->ki_pos;
4765 const size_t count = iov_iter_count(iter);
4766 struct f2fs_map_blocks map = {};
4767 int flag;
4768 int ret;
4769
4770 /* If it will be an out-of-place direct write, don't bother. */
4771 if (dio && f2fs_lfs_mode(sbi))
4772 return 0;
4773 /*
4774 * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
4775 * buffered IO, if DIO meets any holes.
4776 */
4777 if (dio && i_size_read(inode) &&
4778 (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode))))
4779 return 0;
4780
4781 /* No-wait I/O can't allocate blocks. */
4782 if (iocb->ki_flags & IOCB_NOWAIT)
4783 return 0;
4784
4785 /* If it will be a short write, don't bother. */
4786 if (fault_in_iov_iter_readable(iter, count))
4787 return 0;
4788
4789 if (f2fs_has_inline_data(inode)) {
4790 /* If the data will fit inline, don't bother. */
4791 if (pos + count <= MAX_INLINE_DATA(inode))
4792 return 0;
4793 ret = f2fs_convert_inline_inode(inode);
4794 if (ret)
4795 return ret;
4796 }
4797
4798 /* Do not preallocate blocks that will be written partially in 4KB. */
4799 map.m_lblk = F2FS_BLK_ALIGN(pos);
4800 map.m_len = F2FS_BYTES_TO_BLK(pos + count);
4801 if (map.m_len > map.m_lblk)
4802 map.m_len -= map.m_lblk;
4803 else
4804 return 0;
4805
4806 if (!IS_DEVICE_ALIASING(inode))
4807 map.m_may_create = true;
4808 if (dio) {
4809 map.m_seg_type = f2fs_rw_hint_to_seg_type(sbi,
4810 inode->i_write_hint);
4811 flag = F2FS_GET_BLOCK_PRE_DIO;
4812 } else {
4813 map.m_seg_type = NO_CHECK_TYPE;
4814 flag = F2FS_GET_BLOCK_PRE_AIO;
4815 }
4816
4817 ret = f2fs_map_blocks(inode, &map, flag);
4818 /* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */
4819 if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
4820 return ret;
4821 if (ret == 0)
4822 set_inode_flag(inode, FI_PREALLOCATED_ALL);
4823 return map.m_len;
4824}
4825
4826static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
4827 struct iov_iter *from)
4828{
4829 struct file *file = iocb->ki_filp;
4830 struct inode *inode = file_inode(file);
4831 ssize_t ret;
4832
4833 if (iocb->ki_flags & IOCB_NOWAIT)
4834 return -EOPNOTSUPP;
4835
4836 ret = generic_perform_write(iocb, from);
4837
4838 if (ret > 0) {
4839 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4840 APP_BUFFERED_IO, ret);
4841 }
4842 return ret;
4843}
4844
4845static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
4846 unsigned int flags)
4847{
4848 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4849
4850 dec_page_count(sbi, F2FS_DIO_WRITE);
4851 if (error)
4852 return error;
4853 f2fs_update_time(sbi, REQ_TIME);
4854 f2fs_update_iostat(sbi, NULL, APP_DIRECT_IO, size);
4855 return 0;
4856}
4857
4858static void f2fs_dio_write_submit_io(const struct iomap_iter *iter,
4859 struct bio *bio, loff_t file_offset)
4860{
4861 struct inode *inode = iter->inode;
4862 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4863 enum log_type type = f2fs_rw_hint_to_seg_type(sbi, inode->i_write_hint);
4864 enum temp_type temp = f2fs_get_segment_temp(sbi, type);
4865
4866 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, DATA, temp);
4867 submit_bio(bio);
4868}
4869
4870static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
4871 .end_io = f2fs_dio_write_end_io,
4872 .submit_io = f2fs_dio_write_submit_io,
4873};
4874
4875static void f2fs_flush_buffered_write(struct address_space *mapping,
4876 loff_t start_pos, loff_t end_pos)
4877{
4878 int ret;
4879
4880 ret = filemap_write_and_wait_range(mapping, start_pos, end_pos);
4881 if (ret < 0)
4882 return;
4883 invalidate_mapping_pages(mapping,
4884 start_pos >> PAGE_SHIFT,
4885 end_pos >> PAGE_SHIFT);
4886}
4887
4888static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
4889 bool *may_need_sync)
4890{
4891 struct file *file = iocb->ki_filp;
4892 struct inode *inode = file_inode(file);
4893 struct f2fs_inode_info *fi = F2FS_I(inode);
4894 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4895 const bool do_opu = f2fs_lfs_mode(sbi);
4896 const loff_t pos = iocb->ki_pos;
4897 const ssize_t count = iov_iter_count(from);
4898 unsigned int dio_flags;
4899 struct iomap_dio *dio;
4900 ssize_t ret;
4901
4902 trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
4903
4904 if (iocb->ki_flags & IOCB_NOWAIT) {
4905 /* f2fs_convert_inline_inode() and block allocation can block */
4906 if (f2fs_has_inline_data(inode) ||
4907 !f2fs_overwrite_io(inode, pos, count)) {
4908 ret = -EAGAIN;
4909 goto out;
4910 }
4911
4912 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
4913 ret = -EAGAIN;
4914 goto out;
4915 }
4916 if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4917 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4918 ret = -EAGAIN;
4919 goto out;
4920 }
4921 } else {
4922 ret = f2fs_convert_inline_inode(inode);
4923 if (ret)
4924 goto out;
4925
4926 f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
4927 if (do_opu)
4928 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4929 }
4930
4931 /*
4932 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4933 * the higher-level function iomap_dio_rw() in order to ensure that the
4934 * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
4935 */
4936 inc_page_count(sbi, F2FS_DIO_WRITE);
4937 dio_flags = 0;
4938 if (pos + count > inode->i_size)
4939 dio_flags |= IOMAP_DIO_FORCE_WAIT;
4940 dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
4941 &f2fs_iomap_dio_write_ops, dio_flags, NULL, 0);
4942 if (IS_ERR_OR_NULL(dio)) {
4943 ret = PTR_ERR_OR_ZERO(dio);
4944 if (ret == -ENOTBLK)
4945 ret = 0;
4946 if (ret != -EIOCBQUEUED)
4947 dec_page_count(sbi, F2FS_DIO_WRITE);
4948 } else {
4949 ret = iomap_dio_complete(dio);
4950 }
4951
4952 if (do_opu)
4953 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4954 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4955
4956 if (ret < 0)
4957 goto out;
4958 if (pos + ret > inode->i_size)
4959 f2fs_i_size_write(inode, pos + ret);
4960 if (!do_opu)
4961 set_inode_flag(inode, FI_UPDATE_WRITE);
4962
4963 if (iov_iter_count(from)) {
4964 ssize_t ret2;
4965 loff_t bufio_start_pos = iocb->ki_pos;
4966
4967 /*
4968 * The direct write was partial, so we need to fall back to a
4969 * buffered write for the remainder.
4970 */
4971
4972 ret2 = f2fs_buffered_write_iter(iocb, from);
4973 if (iov_iter_count(from))
4974 f2fs_write_failed(inode, iocb->ki_pos);
4975 if (ret2 < 0)
4976 goto out;
4977
4978 /*
4979 * Ensure that the pagecache pages are written to disk and
4980 * invalidated to preserve the expected O_DIRECT semantics.
4981 */
4982 if (ret2 > 0) {
4983 loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
4984
4985 ret += ret2;
4986
4987 f2fs_flush_buffered_write(file->f_mapping,
4988 bufio_start_pos,
4989 bufio_end_pos);
4990 }
4991 } else {
4992 /* iomap_dio_rw() already handled the generic_write_sync(). */
4993 *may_need_sync = false;
4994 }
4995out:
4996 trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
4997 return ret;
4998}
4999
5000static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
5001{
5002 struct inode *inode = file_inode(iocb->ki_filp);
5003 const loff_t orig_pos = iocb->ki_pos;
5004 const size_t orig_count = iov_iter_count(from);
5005 loff_t target_size;
5006 bool dio;
5007 bool may_need_sync = true;
5008 int preallocated;
5009 const loff_t pos = iocb->ki_pos;
5010 const ssize_t count = iov_iter_count(from);
5011 ssize_t ret;
5012
5013 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
5014 ret = -EIO;
5015 goto out;
5016 }
5017
5018 if (!f2fs_is_compress_backend_ready(inode)) {
5019 ret = -EOPNOTSUPP;
5020 goto out;
5021 }
5022
5023 if (iocb->ki_flags & IOCB_NOWAIT) {
5024 if (!inode_trylock(inode)) {
5025 ret = -EAGAIN;
5026 goto out;
5027 }
5028 } else {
5029 inode_lock(inode);
5030 }
5031
5032 if (f2fs_is_pinned_file(inode) &&
5033 !f2fs_overwrite_io(inode, pos, count)) {
5034 ret = -EIO;
5035 goto out_unlock;
5036 }
5037
5038 ret = f2fs_write_checks(iocb, from);
5039 if (ret <= 0)
5040 goto out_unlock;
5041
5042 /* Determine whether we will do a direct write or a buffered write. */
5043 dio = f2fs_should_use_dio(inode, iocb, from);
5044
5045 /* dio is not compatible w/ atomic write */
5046 if (dio && f2fs_is_atomic_file(inode)) {
5047 ret = -EOPNOTSUPP;
5048 goto out_unlock;
5049 }
5050
5051 /* Possibly preallocate the blocks for the write. */
5052 target_size = iocb->ki_pos + iov_iter_count(from);
5053 preallocated = f2fs_preallocate_blocks(iocb, from, dio);
5054 if (preallocated < 0) {
5055 ret = preallocated;
5056 } else {
5057 if (trace_f2fs_datawrite_start_enabled())
5058 f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
5059 orig_count, WRITE);
5060
5061 /* Do the actual write. */
5062 ret = dio ?
5063 f2fs_dio_write_iter(iocb, from, &may_need_sync) :
5064 f2fs_buffered_write_iter(iocb, from);
5065
5066 if (trace_f2fs_datawrite_end_enabled())
5067 trace_f2fs_datawrite_end(inode, orig_pos, ret);
5068 }
5069
5070 /* Don't leave any preallocated blocks around past i_size. */
5071 if (preallocated && i_size_read(inode) < target_size) {
5072 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
5073 filemap_invalidate_lock(inode->i_mapping);
5074 if (!f2fs_truncate(inode))
5075 file_dont_truncate(inode);
5076 filemap_invalidate_unlock(inode->i_mapping);
5077 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
5078 } else {
5079 file_dont_truncate(inode);
5080 }
5081
5082 clear_inode_flag(inode, FI_PREALLOCATED_ALL);
5083out_unlock:
5084 inode_unlock(inode);
5085out:
5086 trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
5087
5088 if (ret > 0 && may_need_sync)
5089 ret = generic_write_sync(iocb, ret);
5090
5091 /* If buffered IO was forced, flush and drop the data from
5092 * the page cache to preserve O_DIRECT semantics
5093 */
5094 if (ret > 0 && !dio && (iocb->ki_flags & IOCB_DIRECT))
5095 f2fs_flush_buffered_write(iocb->ki_filp->f_mapping,
5096 orig_pos,
5097 orig_pos + ret - 1);
5098
5099 return ret;
5100}
5101
5102static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
5103 int advice)
5104{
5105 struct address_space *mapping;
5106 struct backing_dev_info *bdi;
5107 struct inode *inode = file_inode(filp);
5108 int err;
5109
5110 if (advice == POSIX_FADV_SEQUENTIAL) {
5111 if (S_ISFIFO(inode->i_mode))
5112 return -ESPIPE;
5113
5114 mapping = filp->f_mapping;
5115 if (!mapping || len < 0)
5116 return -EINVAL;
5117
5118 bdi = inode_to_bdi(mapping->host);
5119 filp->f_ra.ra_pages = bdi->ra_pages *
5120 F2FS_I_SB(inode)->seq_file_ra_mul;
5121 spin_lock(&filp->f_lock);
5122 filp->f_mode &= ~FMODE_RANDOM;
5123 spin_unlock(&filp->f_lock);
5124 return 0;
5125 } else if (advice == POSIX_FADV_WILLNEED && offset == 0) {
5126 /* Load extent cache at the first readahead. */
5127 f2fs_precache_extents(inode);
5128 }
5129
5130 err = generic_fadvise(filp, offset, len, advice);
5131 if (!err && advice == POSIX_FADV_DONTNEED &&
5132 test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
5133 f2fs_compressed_file(inode))
5134 f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
5135
5136 return err;
5137}
5138
5139#ifdef CONFIG_COMPAT
5140struct compat_f2fs_gc_range {
5141 u32 sync;
5142 compat_u64 start;
5143 compat_u64 len;
5144};
5145#define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
5146 struct compat_f2fs_gc_range)
5147
5148static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
5149{
5150 struct compat_f2fs_gc_range __user *urange;
5151 struct f2fs_gc_range range;
5152 int err;
5153
5154 urange = compat_ptr(arg);
5155 err = get_user(range.sync, &urange->sync);
5156 err |= get_user(range.start, &urange->start);
5157 err |= get_user(range.len, &urange->len);
5158 if (err)
5159 return -EFAULT;
5160
5161 return __f2fs_ioc_gc_range(file, &range);
5162}
5163
5164struct compat_f2fs_move_range {
5165 u32 dst_fd;
5166 compat_u64 pos_in;
5167 compat_u64 pos_out;
5168 compat_u64 len;
5169};
5170#define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
5171 struct compat_f2fs_move_range)
5172
5173static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
5174{
5175 struct compat_f2fs_move_range __user *urange;
5176 struct f2fs_move_range range;
5177 int err;
5178
5179 urange = compat_ptr(arg);
5180 err = get_user(range.dst_fd, &urange->dst_fd);
5181 err |= get_user(range.pos_in, &urange->pos_in);
5182 err |= get_user(range.pos_out, &urange->pos_out);
5183 err |= get_user(range.len, &urange->len);
5184 if (err)
5185 return -EFAULT;
5186
5187 return __f2fs_ioc_move_range(file, &range);
5188}
5189
5190long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5191{
5192 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
5193 return -EIO;
5194 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
5195 return -ENOSPC;
5196
5197 switch (cmd) {
5198 case FS_IOC32_GETVERSION:
5199 cmd = FS_IOC_GETVERSION;
5200 break;
5201 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
5202 return f2fs_compat_ioc_gc_range(file, arg);
5203 case F2FS_IOC32_MOVE_RANGE:
5204 return f2fs_compat_ioc_move_range(file, arg);
5205 case F2FS_IOC_START_ATOMIC_WRITE:
5206 case F2FS_IOC_START_ATOMIC_REPLACE:
5207 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
5208 case F2FS_IOC_START_VOLATILE_WRITE:
5209 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
5210 case F2FS_IOC_ABORT_ATOMIC_WRITE:
5211 case F2FS_IOC_SHUTDOWN:
5212 case FITRIM:
5213 case FS_IOC_SET_ENCRYPTION_POLICY:
5214 case FS_IOC_GET_ENCRYPTION_PWSALT:
5215 case FS_IOC_GET_ENCRYPTION_POLICY:
5216 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
5217 case FS_IOC_ADD_ENCRYPTION_KEY:
5218 case FS_IOC_REMOVE_ENCRYPTION_KEY:
5219 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
5220 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
5221 case FS_IOC_GET_ENCRYPTION_NONCE:
5222 case F2FS_IOC_GARBAGE_COLLECT:
5223 case F2FS_IOC_WRITE_CHECKPOINT:
5224 case F2FS_IOC_DEFRAGMENT:
5225 case F2FS_IOC_FLUSH_DEVICE:
5226 case F2FS_IOC_GET_FEATURES:
5227 case F2FS_IOC_GET_PIN_FILE:
5228 case F2FS_IOC_SET_PIN_FILE:
5229 case F2FS_IOC_PRECACHE_EXTENTS:
5230 case F2FS_IOC_RESIZE_FS:
5231 case FS_IOC_ENABLE_VERITY:
5232 case FS_IOC_MEASURE_VERITY:
5233 case FS_IOC_READ_VERITY_METADATA:
5234 case FS_IOC_GETFSLABEL:
5235 case FS_IOC_SETFSLABEL:
5236 case F2FS_IOC_GET_COMPRESS_BLOCKS:
5237 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
5238 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
5239 case F2FS_IOC_SEC_TRIM_FILE:
5240 case F2FS_IOC_GET_COMPRESS_OPTION:
5241 case F2FS_IOC_SET_COMPRESS_OPTION:
5242 case F2FS_IOC_DECOMPRESS_FILE:
5243 case F2FS_IOC_COMPRESS_FILE:
5244 case F2FS_IOC_GET_DEV_ALIAS_FILE:
5245 break;
5246 default:
5247 return -ENOIOCTLCMD;
5248 }
5249 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5250}
5251#endif
5252
5253const struct file_operations f2fs_file_operations = {
5254 .llseek = f2fs_llseek,
5255 .read_iter = f2fs_file_read_iter,
5256 .write_iter = f2fs_file_write_iter,
5257 .iopoll = iocb_bio_iopoll,
5258 .open = f2fs_file_open,
5259 .release = f2fs_release_file,
5260 .mmap = f2fs_file_mmap,
5261 .flush = f2fs_file_flush,
5262 .fsync = f2fs_sync_file,
5263 .fallocate = f2fs_fallocate,
5264 .unlocked_ioctl = f2fs_ioctl,
5265#ifdef CONFIG_COMPAT
5266 .compat_ioctl = f2fs_compat_ioctl,
5267#endif
5268 .splice_read = f2fs_file_splice_read,
5269 .splice_write = iter_file_splice_write,
5270 .fadvise = f2fs_file_fadvise,
5271 .fop_flags = FOP_BUFFER_RASYNC,
5272};
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * fs/f2fs/file.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8#include <linux/fs.h>
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>
23#include <linux/nls.h>
24#include <linux/sched/signal.h>
25#include <linux/fileattr.h>
26#include <linux/fadvise.h>
27#include <linux/iomap.h>
28
29#include "f2fs.h"
30#include "node.h"
31#include "segment.h"
32#include "xattr.h"
33#include "acl.h"
34#include "gc.h"
35#include "iostat.h"
36#include <trace/events/f2fs.h>
37#include <uapi/linux/f2fs.h>
38
39static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
40{
41 struct inode *inode = file_inode(vmf->vma->vm_file);
42 vm_flags_t flags = vmf->vma->vm_flags;
43 vm_fault_t ret;
44
45 ret = filemap_fault(vmf);
46 if (ret & VM_FAULT_LOCKED)
47 f2fs_update_iostat(F2FS_I_SB(inode), inode,
48 APP_MAPPED_READ_IO, F2FS_BLKSIZE);
49
50 trace_f2fs_filemap_fault(inode, vmf->pgoff, flags, ret);
51
52 return ret;
53}
54
55static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
56{
57 struct page *page = vmf->page;
58 struct inode *inode = file_inode(vmf->vma->vm_file);
59 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
60 struct dnode_of_data dn;
61 bool need_alloc = true;
62 int err = 0;
63 vm_fault_t ret;
64
65 if (unlikely(IS_IMMUTABLE(inode)))
66 return VM_FAULT_SIGBUS;
67
68 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
69 err = -EIO;
70 goto out;
71 }
72
73 if (unlikely(f2fs_cp_error(sbi))) {
74 err = -EIO;
75 goto out;
76 }
77
78 if (!f2fs_is_checkpoint_ready(sbi)) {
79 err = -ENOSPC;
80 goto out;
81 }
82
83 err = f2fs_convert_inline_inode(inode);
84 if (err)
85 goto out;
86
87#ifdef CONFIG_F2FS_FS_COMPRESSION
88 if (f2fs_compressed_file(inode)) {
89 int ret = f2fs_is_compressed_cluster(inode, page->index);
90
91 if (ret < 0) {
92 err = ret;
93 goto out;
94 } else if (ret) {
95 need_alloc = false;
96 }
97 }
98#endif
99 /* should do out of any locked page */
100 if (need_alloc)
101 f2fs_balance_fs(sbi, true);
102
103 sb_start_pagefault(inode->i_sb);
104
105 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
106
107 file_update_time(vmf->vma->vm_file);
108 filemap_invalidate_lock_shared(inode->i_mapping);
109 lock_page(page);
110 if (unlikely(page->mapping != inode->i_mapping ||
111 page_offset(page) > i_size_read(inode) ||
112 !PageUptodate(page))) {
113 unlock_page(page);
114 err = -EFAULT;
115 goto out_sem;
116 }
117
118 if (need_alloc) {
119 /* block allocation */
120 set_new_dnode(&dn, inode, NULL, NULL, 0);
121 err = f2fs_get_block_locked(&dn, page->index);
122 }
123
124#ifdef CONFIG_F2FS_FS_COMPRESSION
125 if (!need_alloc) {
126 set_new_dnode(&dn, inode, NULL, NULL, 0);
127 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
128 f2fs_put_dnode(&dn);
129 }
130#endif
131 if (err) {
132 unlock_page(page);
133 goto out_sem;
134 }
135
136 f2fs_wait_on_page_writeback(page, DATA, false, true);
137
138 /* wait for GCed page writeback via META_MAPPING */
139 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
140
141 /*
142 * check to see if the page is mapped already (no holes)
143 */
144 if (PageMappedToDisk(page))
145 goto out_sem;
146
147 /* page is wholly or partially inside EOF */
148 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
149 i_size_read(inode)) {
150 loff_t offset;
151
152 offset = i_size_read(inode) & ~PAGE_MASK;
153 zero_user_segment(page, offset, PAGE_SIZE);
154 }
155 set_page_dirty(page);
156
157 f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE);
158 f2fs_update_time(sbi, REQ_TIME);
159
160out_sem:
161 filemap_invalidate_unlock_shared(inode->i_mapping);
162
163 sb_end_pagefault(inode->i_sb);
164out:
165 ret = vmf_fs_error(err);
166
167 trace_f2fs_vm_page_mkwrite(inode, page->index, vmf->vma->vm_flags, ret);
168 return ret;
169}
170
171static const struct vm_operations_struct f2fs_file_vm_ops = {
172 .fault = f2fs_filemap_fault,
173 .map_pages = filemap_map_pages,
174 .page_mkwrite = f2fs_vm_page_mkwrite,
175};
176
177static int get_parent_ino(struct inode *inode, nid_t *pino)
178{
179 struct dentry *dentry;
180
181 /*
182 * Make sure to get the non-deleted alias. The alias associated with
183 * the open file descriptor being fsync()'ed may be deleted already.
184 */
185 dentry = d_find_alias(inode);
186 if (!dentry)
187 return 0;
188
189 *pino = parent_ino(dentry);
190 dput(dentry);
191 return 1;
192}
193
194static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
195{
196 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
197 enum cp_reason_type cp_reason = CP_NO_NEEDED;
198
199 if (!S_ISREG(inode->i_mode))
200 cp_reason = CP_NON_REGULAR;
201 else if (f2fs_compressed_file(inode))
202 cp_reason = CP_COMPRESSED;
203 else if (inode->i_nlink != 1)
204 cp_reason = CP_HARDLINK;
205 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
206 cp_reason = CP_SB_NEED_CP;
207 else if (file_wrong_pino(inode))
208 cp_reason = CP_WRONG_PINO;
209 else if (!f2fs_space_for_roll_forward(sbi))
210 cp_reason = CP_NO_SPC_ROLL;
211 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
212 cp_reason = CP_NODE_NEED_CP;
213 else if (test_opt(sbi, FASTBOOT))
214 cp_reason = CP_FASTBOOT_MODE;
215 else if (F2FS_OPTION(sbi).active_logs == 2)
216 cp_reason = CP_SPEC_LOG_NUM;
217 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
218 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
219 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
220 TRANS_DIR_INO))
221 cp_reason = CP_RECOVER_DIR;
222
223 return cp_reason;
224}
225
226static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
227{
228 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
229 bool ret = false;
230 /* But we need to avoid that there are some inode updates */
231 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
232 ret = true;
233 f2fs_put_page(i, 0);
234 return ret;
235}
236
237static void try_to_fix_pino(struct inode *inode)
238{
239 struct f2fs_inode_info *fi = F2FS_I(inode);
240 nid_t pino;
241
242 f2fs_down_write(&fi->i_sem);
243 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
244 get_parent_ino(inode, &pino)) {
245 f2fs_i_pino_write(inode, pino);
246 file_got_pino(inode);
247 }
248 f2fs_up_write(&fi->i_sem);
249}
250
251static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
252 int datasync, bool atomic)
253{
254 struct inode *inode = file->f_mapping->host;
255 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
256 nid_t ino = inode->i_ino;
257 int ret = 0;
258 enum cp_reason_type cp_reason = 0;
259 struct writeback_control wbc = {
260 .sync_mode = WB_SYNC_ALL,
261 .nr_to_write = LONG_MAX,
262 .for_reclaim = 0,
263 };
264 unsigned int seq_id = 0;
265
266 if (unlikely(f2fs_readonly(inode->i_sb)))
267 return 0;
268
269 trace_f2fs_sync_file_enter(inode);
270
271 if (S_ISDIR(inode->i_mode))
272 goto go_write;
273
274 /* if fdatasync is triggered, let's do in-place-update */
275 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
276 set_inode_flag(inode, FI_NEED_IPU);
277 ret = file_write_and_wait_range(file, start, end);
278 clear_inode_flag(inode, FI_NEED_IPU);
279
280 if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
281 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
282 return ret;
283 }
284
285 /* if the inode is dirty, let's recover all the time */
286 if (!f2fs_skip_inode_update(inode, datasync)) {
287 f2fs_write_inode(inode, NULL);
288 goto go_write;
289 }
290
291 /*
292 * if there is no written data, don't waste time to write recovery info.
293 */
294 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
295 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
296
297 /* it may call write_inode just prior to fsync */
298 if (need_inode_page_update(sbi, ino))
299 goto go_write;
300
301 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
302 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
303 goto flush_out;
304 goto out;
305 } else {
306 /*
307 * for OPU case, during fsync(), node can be persisted before
308 * data when lower device doesn't support write barrier, result
309 * in data corruption after SPO.
310 * So for strict fsync mode, force to use atomic write semantics
311 * to keep write order in between data/node and last node to
312 * avoid potential data corruption.
313 */
314 if (F2FS_OPTION(sbi).fsync_mode ==
315 FSYNC_MODE_STRICT && !atomic)
316 atomic = true;
317 }
318go_write:
319 /*
320 * Both of fdatasync() and fsync() are able to be recovered from
321 * sudden-power-off.
322 */
323 f2fs_down_read(&F2FS_I(inode)->i_sem);
324 cp_reason = need_do_checkpoint(inode);
325 f2fs_up_read(&F2FS_I(inode)->i_sem);
326
327 if (cp_reason) {
328 /* all the dirty node pages should be flushed for POR */
329 ret = f2fs_sync_fs(inode->i_sb, 1);
330
331 /*
332 * We've secured consistency through sync_fs. Following pino
333 * will be used only for fsynced inodes after checkpoint.
334 */
335 try_to_fix_pino(inode);
336 clear_inode_flag(inode, FI_APPEND_WRITE);
337 clear_inode_flag(inode, FI_UPDATE_WRITE);
338 goto out;
339 }
340sync_nodes:
341 atomic_inc(&sbi->wb_sync_req[NODE]);
342 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
343 atomic_dec(&sbi->wb_sync_req[NODE]);
344 if (ret)
345 goto out;
346
347 /* if cp_error was enabled, we should avoid infinite loop */
348 if (unlikely(f2fs_cp_error(sbi))) {
349 ret = -EIO;
350 goto out;
351 }
352
353 if (f2fs_need_inode_block_update(sbi, ino)) {
354 f2fs_mark_inode_dirty_sync(inode, true);
355 f2fs_write_inode(inode, NULL);
356 goto sync_nodes;
357 }
358
359 /*
360 * If it's atomic_write, it's just fine to keep write ordering. So
361 * here we don't need to wait for node write completion, since we use
362 * node chain which serializes node blocks. If one of node writes are
363 * reordered, we can see simply broken chain, resulting in stopping
364 * roll-forward recovery. It means we'll recover all or none node blocks
365 * given fsync mark.
366 */
367 if (!atomic) {
368 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
369 if (ret)
370 goto out;
371 }
372
373 /* once recovery info is written, don't need to tack this */
374 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
375 clear_inode_flag(inode, FI_APPEND_WRITE);
376flush_out:
377 if ((!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER) ||
378 (atomic && !test_opt(sbi, NOBARRIER) && f2fs_sb_has_blkzoned(sbi)))
379 ret = f2fs_issue_flush(sbi, inode->i_ino);
380 if (!ret) {
381 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
382 clear_inode_flag(inode, FI_UPDATE_WRITE);
383 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
384 }
385 f2fs_update_time(sbi, REQ_TIME);
386out:
387 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
388 return ret;
389}
390
391int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
392{
393 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
394 return -EIO;
395 return f2fs_do_sync_file(file, start, end, datasync, false);
396}
397
398static bool __found_offset(struct address_space *mapping,
399 struct dnode_of_data *dn, pgoff_t index, int whence)
400{
401 block_t blkaddr = f2fs_data_blkaddr(dn);
402 struct inode *inode = mapping->host;
403 bool compressed_cluster = false;
404
405 if (f2fs_compressed_file(inode)) {
406 block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_page,
407 ALIGN_DOWN(dn->ofs_in_node, F2FS_I(inode)->i_cluster_size));
408
409 compressed_cluster = first_blkaddr == COMPRESS_ADDR;
410 }
411
412 switch (whence) {
413 case SEEK_DATA:
414 if (__is_valid_data_blkaddr(blkaddr))
415 return true;
416 if (blkaddr == NEW_ADDR &&
417 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
418 return true;
419 if (compressed_cluster)
420 return true;
421 break;
422 case SEEK_HOLE:
423 if (compressed_cluster)
424 return false;
425 if (blkaddr == NULL_ADDR)
426 return true;
427 break;
428 }
429 return false;
430}
431
432static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
433{
434 struct inode *inode = file->f_mapping->host;
435 loff_t maxbytes = inode->i_sb->s_maxbytes;
436 struct dnode_of_data dn;
437 pgoff_t pgofs, end_offset;
438 loff_t data_ofs = offset;
439 loff_t isize;
440 int err = 0;
441
442 inode_lock_shared(inode);
443
444 isize = i_size_read(inode);
445 if (offset >= isize)
446 goto fail;
447
448 /* handle inline data case */
449 if (f2fs_has_inline_data(inode)) {
450 if (whence == SEEK_HOLE) {
451 data_ofs = isize;
452 goto found;
453 } else if (whence == SEEK_DATA) {
454 data_ofs = offset;
455 goto found;
456 }
457 }
458
459 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
460
461 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
462 set_new_dnode(&dn, inode, NULL, NULL, 0);
463 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
464 if (err && err != -ENOENT) {
465 goto fail;
466 } else if (err == -ENOENT) {
467 /* direct node does not exists */
468 if (whence == SEEK_DATA) {
469 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
470 continue;
471 } else {
472 goto found;
473 }
474 }
475
476 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
477
478 /* find data/hole in dnode block */
479 for (; dn.ofs_in_node < end_offset;
480 dn.ofs_in_node++, pgofs++,
481 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
482 block_t blkaddr;
483
484 blkaddr = f2fs_data_blkaddr(&dn);
485
486 if (__is_valid_data_blkaddr(blkaddr) &&
487 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
488 blkaddr, DATA_GENERIC_ENHANCE)) {
489 f2fs_put_dnode(&dn);
490 goto fail;
491 }
492
493 if (__found_offset(file->f_mapping, &dn,
494 pgofs, whence)) {
495 f2fs_put_dnode(&dn);
496 goto found;
497 }
498 }
499 f2fs_put_dnode(&dn);
500 }
501
502 if (whence == SEEK_DATA)
503 goto fail;
504found:
505 if (whence == SEEK_HOLE && data_ofs > isize)
506 data_ofs = isize;
507 inode_unlock_shared(inode);
508 return vfs_setpos(file, data_ofs, maxbytes);
509fail:
510 inode_unlock_shared(inode);
511 return -ENXIO;
512}
513
514static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
515{
516 struct inode *inode = file->f_mapping->host;
517 loff_t maxbytes = inode->i_sb->s_maxbytes;
518
519 if (f2fs_compressed_file(inode))
520 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
521
522 switch (whence) {
523 case SEEK_SET:
524 case SEEK_CUR:
525 case SEEK_END:
526 return generic_file_llseek_size(file, offset, whence,
527 maxbytes, i_size_read(inode));
528 case SEEK_DATA:
529 case SEEK_HOLE:
530 if (offset < 0)
531 return -ENXIO;
532 return f2fs_seek_block(file, offset, whence);
533 }
534
535 return -EINVAL;
536}
537
538static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
539{
540 struct inode *inode = file_inode(file);
541
542 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
543 return -EIO;
544
545 if (!f2fs_is_compress_backend_ready(inode))
546 return -EOPNOTSUPP;
547
548 file_accessed(file);
549 vma->vm_ops = &f2fs_file_vm_ops;
550
551 f2fs_down_read(&F2FS_I(inode)->i_sem);
552 set_inode_flag(inode, FI_MMAP_FILE);
553 f2fs_up_read(&F2FS_I(inode)->i_sem);
554
555 return 0;
556}
557
558static int f2fs_file_open(struct inode *inode, struct file *filp)
559{
560 int err = fscrypt_file_open(inode, filp);
561
562 if (err)
563 return err;
564
565 if (!f2fs_is_compress_backend_ready(inode))
566 return -EOPNOTSUPP;
567
568 err = fsverity_file_open(inode, filp);
569 if (err)
570 return err;
571
572 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
573 filp->f_mode |= FMODE_CAN_ODIRECT;
574
575 return dquot_file_open(inode, filp);
576}
577
578void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
579{
580 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
581 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
582 __le32 *addr;
583 bool compressed_cluster = false;
584 int cluster_index = 0, valid_blocks = 0;
585 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
586 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
587
588 addr = get_dnode_addr(dn->inode, dn->node_page) + ofs;
589
590 /* Assumption: truncation starts with cluster */
591 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
592 block_t blkaddr = le32_to_cpu(*addr);
593
594 if (f2fs_compressed_file(dn->inode) &&
595 !(cluster_index & (cluster_size - 1))) {
596 if (compressed_cluster)
597 f2fs_i_compr_blocks_update(dn->inode,
598 valid_blocks, false);
599 compressed_cluster = (blkaddr == COMPRESS_ADDR);
600 valid_blocks = 0;
601 }
602
603 if (blkaddr == NULL_ADDR)
604 continue;
605
606 f2fs_set_data_blkaddr(dn, NULL_ADDR);
607
608 if (__is_valid_data_blkaddr(blkaddr)) {
609 if (time_to_inject(sbi, FAULT_BLKADDR_CONSISTENCE))
610 continue;
611 if (!f2fs_is_valid_blkaddr_raw(sbi, blkaddr,
612 DATA_GENERIC_ENHANCE))
613 continue;
614 if (compressed_cluster)
615 valid_blocks++;
616 }
617
618 f2fs_invalidate_blocks(sbi, blkaddr);
619
620 if (!released || blkaddr != COMPRESS_ADDR)
621 nr_free++;
622 }
623
624 if (compressed_cluster)
625 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
626
627 if (nr_free) {
628 pgoff_t fofs;
629 /*
630 * once we invalidate valid blkaddr in range [ofs, ofs + count],
631 * we will invalidate all blkaddr in the whole range.
632 */
633 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
634 dn->inode) + ofs;
635 f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
636 f2fs_update_age_extent_cache_range(dn, fofs, len);
637 dec_valid_block_count(sbi, dn->inode, nr_free);
638 }
639 dn->ofs_in_node = ofs;
640
641 f2fs_update_time(sbi, REQ_TIME);
642 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
643 dn->ofs_in_node, nr_free);
644}
645
646static int truncate_partial_data_page(struct inode *inode, u64 from,
647 bool cache_only)
648{
649 loff_t offset = from & (PAGE_SIZE - 1);
650 pgoff_t index = from >> PAGE_SHIFT;
651 struct address_space *mapping = inode->i_mapping;
652 struct page *page;
653
654 if (!offset && !cache_only)
655 return 0;
656
657 if (cache_only) {
658 page = find_lock_page(mapping, index);
659 if (page && PageUptodate(page))
660 goto truncate_out;
661 f2fs_put_page(page, 1);
662 return 0;
663 }
664
665 page = f2fs_get_lock_data_page(inode, index, true);
666 if (IS_ERR(page))
667 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
668truncate_out:
669 f2fs_wait_on_page_writeback(page, DATA, true, true);
670 zero_user(page, offset, PAGE_SIZE - offset);
671
672 /* An encrypted inode should have a key and truncate the last page. */
673 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
674 if (!cache_only)
675 set_page_dirty(page);
676 f2fs_put_page(page, 1);
677 return 0;
678}
679
680int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
681{
682 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
683 struct dnode_of_data dn;
684 pgoff_t free_from;
685 int count = 0, err = 0;
686 struct page *ipage;
687 bool truncate_page = false;
688
689 trace_f2fs_truncate_blocks_enter(inode, from);
690
691 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
692
693 if (free_from >= max_file_blocks(inode))
694 goto free_partial;
695
696 if (lock)
697 f2fs_lock_op(sbi);
698
699 ipage = f2fs_get_node_page(sbi, inode->i_ino);
700 if (IS_ERR(ipage)) {
701 err = PTR_ERR(ipage);
702 goto out;
703 }
704
705 if (f2fs_has_inline_data(inode)) {
706 f2fs_truncate_inline_inode(inode, ipage, from);
707 f2fs_put_page(ipage, 1);
708 truncate_page = true;
709 goto out;
710 }
711
712 set_new_dnode(&dn, inode, ipage, NULL, 0);
713 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
714 if (err) {
715 if (err == -ENOENT)
716 goto free_next;
717 goto out;
718 }
719
720 count = ADDRS_PER_PAGE(dn.node_page, inode);
721
722 count -= dn.ofs_in_node;
723 f2fs_bug_on(sbi, count < 0);
724
725 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
726 f2fs_truncate_data_blocks_range(&dn, count);
727 free_from += count;
728 }
729
730 f2fs_put_dnode(&dn);
731free_next:
732 err = f2fs_truncate_inode_blocks(inode, free_from);
733out:
734 if (lock)
735 f2fs_unlock_op(sbi);
736free_partial:
737 /* lastly zero out the first data page */
738 if (!err)
739 err = truncate_partial_data_page(inode, from, truncate_page);
740
741 trace_f2fs_truncate_blocks_exit(inode, err);
742 return err;
743}
744
745int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
746{
747 u64 free_from = from;
748 int err;
749
750#ifdef CONFIG_F2FS_FS_COMPRESSION
751 /*
752 * for compressed file, only support cluster size
753 * aligned truncation.
754 */
755 if (f2fs_compressed_file(inode))
756 free_from = round_up(from,
757 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
758#endif
759
760 err = f2fs_do_truncate_blocks(inode, free_from, lock);
761 if (err)
762 return err;
763
764#ifdef CONFIG_F2FS_FS_COMPRESSION
765 /*
766 * For compressed file, after release compress blocks, don't allow write
767 * direct, but we should allow write direct after truncate to zero.
768 */
769 if (f2fs_compressed_file(inode) && !free_from
770 && is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
771 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
772
773 if (from != free_from) {
774 err = f2fs_truncate_partial_cluster(inode, from, lock);
775 if (err)
776 return err;
777 }
778#endif
779
780 return 0;
781}
782
783int f2fs_truncate(struct inode *inode)
784{
785 int err;
786
787 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
788 return -EIO;
789
790 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
791 S_ISLNK(inode->i_mode)))
792 return 0;
793
794 trace_f2fs_truncate(inode);
795
796 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE))
797 return -EIO;
798
799 err = f2fs_dquot_initialize(inode);
800 if (err)
801 return err;
802
803 /* we should check inline_data size */
804 if (!f2fs_may_inline_data(inode)) {
805 err = f2fs_convert_inline_inode(inode);
806 if (err)
807 return err;
808 }
809
810 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
811 if (err)
812 return err;
813
814 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
815 f2fs_mark_inode_dirty_sync(inode, false);
816 return 0;
817}
818
819static bool f2fs_force_buffered_io(struct inode *inode, int rw)
820{
821 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
822
823 if (!fscrypt_dio_supported(inode))
824 return true;
825 if (fsverity_active(inode))
826 return true;
827 if (f2fs_compressed_file(inode))
828 return true;
829
830 /* disallow direct IO if any of devices has unaligned blksize */
831 if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
832 return true;
833 /*
834 * for blkzoned device, fallback direct IO to buffered IO, so
835 * all IOs can be serialized by log-structured write.
836 */
837 if (f2fs_sb_has_blkzoned(sbi) && (rw == WRITE))
838 return true;
839 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
840 return true;
841
842 return false;
843}
844
845int f2fs_getattr(struct mnt_idmap *idmap, const struct path *path,
846 struct kstat *stat, u32 request_mask, unsigned int query_flags)
847{
848 struct inode *inode = d_inode(path->dentry);
849 struct f2fs_inode_info *fi = F2FS_I(inode);
850 struct f2fs_inode *ri = NULL;
851 unsigned int flags;
852
853 if (f2fs_has_extra_attr(inode) &&
854 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
855 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
856 stat->result_mask |= STATX_BTIME;
857 stat->btime.tv_sec = fi->i_crtime.tv_sec;
858 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
859 }
860
861 /*
862 * Return the DIO alignment restrictions if requested. We only return
863 * this information when requested, since on encrypted files it might
864 * take a fair bit of work to get if the file wasn't opened recently.
865 *
866 * f2fs sometimes supports DIO reads but not DIO writes. STATX_DIOALIGN
867 * cannot represent that, so in that case we report no DIO support.
868 */
869 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
870 unsigned int bsize = i_blocksize(inode);
871
872 stat->result_mask |= STATX_DIOALIGN;
873 if (!f2fs_force_buffered_io(inode, WRITE)) {
874 stat->dio_mem_align = bsize;
875 stat->dio_offset_align = bsize;
876 }
877 }
878
879 flags = fi->i_flags;
880 if (flags & F2FS_COMPR_FL)
881 stat->attributes |= STATX_ATTR_COMPRESSED;
882 if (flags & F2FS_APPEND_FL)
883 stat->attributes |= STATX_ATTR_APPEND;
884 if (IS_ENCRYPTED(inode))
885 stat->attributes |= STATX_ATTR_ENCRYPTED;
886 if (flags & F2FS_IMMUTABLE_FL)
887 stat->attributes |= STATX_ATTR_IMMUTABLE;
888 if (flags & F2FS_NODUMP_FL)
889 stat->attributes |= STATX_ATTR_NODUMP;
890 if (IS_VERITY(inode))
891 stat->attributes |= STATX_ATTR_VERITY;
892
893 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
894 STATX_ATTR_APPEND |
895 STATX_ATTR_ENCRYPTED |
896 STATX_ATTR_IMMUTABLE |
897 STATX_ATTR_NODUMP |
898 STATX_ATTR_VERITY);
899
900 generic_fillattr(idmap, request_mask, inode, stat);
901
902 /* we need to show initial sectors used for inline_data/dentries */
903 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
904 f2fs_has_inline_dentry(inode))
905 stat->blocks += (stat->size + 511) >> 9;
906
907 return 0;
908}
909
910#ifdef CONFIG_F2FS_FS_POSIX_ACL
911static void __setattr_copy(struct mnt_idmap *idmap,
912 struct inode *inode, const struct iattr *attr)
913{
914 unsigned int ia_valid = attr->ia_valid;
915
916 i_uid_update(idmap, attr, inode);
917 i_gid_update(idmap, attr, inode);
918 if (ia_valid & ATTR_ATIME)
919 inode_set_atime_to_ts(inode, attr->ia_atime);
920 if (ia_valid & ATTR_MTIME)
921 inode_set_mtime_to_ts(inode, attr->ia_mtime);
922 if (ia_valid & ATTR_CTIME)
923 inode_set_ctime_to_ts(inode, attr->ia_ctime);
924 if (ia_valid & ATTR_MODE) {
925 umode_t mode = attr->ia_mode;
926 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
927
928 if (!vfsgid_in_group_p(vfsgid) &&
929 !capable_wrt_inode_uidgid(idmap, inode, CAP_FSETID))
930 mode &= ~S_ISGID;
931 set_acl_inode(inode, mode);
932 }
933}
934#else
935#define __setattr_copy setattr_copy
936#endif
937
938int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
939 struct iattr *attr)
940{
941 struct inode *inode = d_inode(dentry);
942 int err;
943
944 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
945 return -EIO;
946
947 if (unlikely(IS_IMMUTABLE(inode)))
948 return -EPERM;
949
950 if (unlikely(IS_APPEND(inode) &&
951 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
952 ATTR_GID | ATTR_TIMES_SET))))
953 return -EPERM;
954
955 if ((attr->ia_valid & ATTR_SIZE)) {
956 if (!f2fs_is_compress_backend_ready(inode))
957 return -EOPNOTSUPP;
958 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED) &&
959 !IS_ALIGNED(attr->ia_size,
960 F2FS_BLK_TO_BYTES(F2FS_I(inode)->i_cluster_size)))
961 return -EINVAL;
962 }
963
964 err = setattr_prepare(idmap, dentry, attr);
965 if (err)
966 return err;
967
968 err = fscrypt_prepare_setattr(dentry, attr);
969 if (err)
970 return err;
971
972 err = fsverity_prepare_setattr(dentry, attr);
973 if (err)
974 return err;
975
976 if (is_quota_modification(idmap, inode, attr)) {
977 err = f2fs_dquot_initialize(inode);
978 if (err)
979 return err;
980 }
981 if (i_uid_needs_update(idmap, attr, inode) ||
982 i_gid_needs_update(idmap, attr, inode)) {
983 f2fs_lock_op(F2FS_I_SB(inode));
984 err = dquot_transfer(idmap, inode, attr);
985 if (err) {
986 set_sbi_flag(F2FS_I_SB(inode),
987 SBI_QUOTA_NEED_REPAIR);
988 f2fs_unlock_op(F2FS_I_SB(inode));
989 return err;
990 }
991 /*
992 * update uid/gid under lock_op(), so that dquot and inode can
993 * be updated atomically.
994 */
995 i_uid_update(idmap, attr, inode);
996 i_gid_update(idmap, attr, inode);
997 f2fs_mark_inode_dirty_sync(inode, true);
998 f2fs_unlock_op(F2FS_I_SB(inode));
999 }
1000
1001 if (attr->ia_valid & ATTR_SIZE) {
1002 loff_t old_size = i_size_read(inode);
1003
1004 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
1005 /*
1006 * should convert inline inode before i_size_write to
1007 * keep smaller than inline_data size with inline flag.
1008 */
1009 err = f2fs_convert_inline_inode(inode);
1010 if (err)
1011 return err;
1012 }
1013
1014 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1015 filemap_invalidate_lock(inode->i_mapping);
1016
1017 truncate_setsize(inode, attr->ia_size);
1018
1019 if (attr->ia_size <= old_size)
1020 err = f2fs_truncate(inode);
1021 /*
1022 * do not trim all blocks after i_size if target size is
1023 * larger than i_size.
1024 */
1025 filemap_invalidate_unlock(inode->i_mapping);
1026 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1027 if (err)
1028 return err;
1029
1030 spin_lock(&F2FS_I(inode)->i_size_lock);
1031 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1032 F2FS_I(inode)->last_disk_size = i_size_read(inode);
1033 spin_unlock(&F2FS_I(inode)->i_size_lock);
1034 }
1035
1036 __setattr_copy(idmap, inode, attr);
1037
1038 if (attr->ia_valid & ATTR_MODE) {
1039 err = posix_acl_chmod(idmap, dentry, f2fs_get_inode_mode(inode));
1040
1041 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
1042 if (!err)
1043 inode->i_mode = F2FS_I(inode)->i_acl_mode;
1044 clear_inode_flag(inode, FI_ACL_MODE);
1045 }
1046 }
1047
1048 /* file size may changed here */
1049 f2fs_mark_inode_dirty_sync(inode, true);
1050
1051 /* inode change will produce dirty node pages flushed by checkpoint */
1052 f2fs_balance_fs(F2FS_I_SB(inode), true);
1053
1054 return err;
1055}
1056
1057const struct inode_operations f2fs_file_inode_operations = {
1058 .getattr = f2fs_getattr,
1059 .setattr = f2fs_setattr,
1060 .get_inode_acl = f2fs_get_acl,
1061 .set_acl = f2fs_set_acl,
1062 .listxattr = f2fs_listxattr,
1063 .fiemap = f2fs_fiemap,
1064 .fileattr_get = f2fs_fileattr_get,
1065 .fileattr_set = f2fs_fileattr_set,
1066};
1067
1068static int fill_zero(struct inode *inode, pgoff_t index,
1069 loff_t start, loff_t len)
1070{
1071 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1072 struct page *page;
1073
1074 if (!len)
1075 return 0;
1076
1077 f2fs_balance_fs(sbi, true);
1078
1079 f2fs_lock_op(sbi);
1080 page = f2fs_get_new_data_page(inode, NULL, index, false);
1081 f2fs_unlock_op(sbi);
1082
1083 if (IS_ERR(page))
1084 return PTR_ERR(page);
1085
1086 f2fs_wait_on_page_writeback(page, DATA, true, true);
1087 zero_user(page, start, len);
1088 set_page_dirty(page);
1089 f2fs_put_page(page, 1);
1090 return 0;
1091}
1092
1093int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1094{
1095 int err;
1096
1097 while (pg_start < pg_end) {
1098 struct dnode_of_data dn;
1099 pgoff_t end_offset, count;
1100
1101 set_new_dnode(&dn, inode, NULL, NULL, 0);
1102 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1103 if (err) {
1104 if (err == -ENOENT) {
1105 pg_start = f2fs_get_next_page_offset(&dn,
1106 pg_start);
1107 continue;
1108 }
1109 return err;
1110 }
1111
1112 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1113 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1114
1115 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1116
1117 f2fs_truncate_data_blocks_range(&dn, count);
1118 f2fs_put_dnode(&dn);
1119
1120 pg_start += count;
1121 }
1122 return 0;
1123}
1124
1125static int f2fs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
1126{
1127 pgoff_t pg_start, pg_end;
1128 loff_t off_start, off_end;
1129 int ret;
1130
1131 ret = f2fs_convert_inline_inode(inode);
1132 if (ret)
1133 return ret;
1134
1135 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1136 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1137
1138 off_start = offset & (PAGE_SIZE - 1);
1139 off_end = (offset + len) & (PAGE_SIZE - 1);
1140
1141 if (pg_start == pg_end) {
1142 ret = fill_zero(inode, pg_start, off_start,
1143 off_end - off_start);
1144 if (ret)
1145 return ret;
1146 } else {
1147 if (off_start) {
1148 ret = fill_zero(inode, pg_start++, off_start,
1149 PAGE_SIZE - off_start);
1150 if (ret)
1151 return ret;
1152 }
1153 if (off_end) {
1154 ret = fill_zero(inode, pg_end, 0, off_end);
1155 if (ret)
1156 return ret;
1157 }
1158
1159 if (pg_start < pg_end) {
1160 loff_t blk_start, blk_end;
1161 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1162
1163 f2fs_balance_fs(sbi, true);
1164
1165 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1166 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1167
1168 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1169 filemap_invalidate_lock(inode->i_mapping);
1170
1171 truncate_pagecache_range(inode, blk_start, blk_end - 1);
1172
1173 f2fs_lock_op(sbi);
1174 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1175 f2fs_unlock_op(sbi);
1176
1177 filemap_invalidate_unlock(inode->i_mapping);
1178 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1179 }
1180 }
1181
1182 return ret;
1183}
1184
1185static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1186 int *do_replace, pgoff_t off, pgoff_t len)
1187{
1188 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1189 struct dnode_of_data dn;
1190 int ret, done, i;
1191
1192next_dnode:
1193 set_new_dnode(&dn, inode, NULL, NULL, 0);
1194 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1195 if (ret && ret != -ENOENT) {
1196 return ret;
1197 } else if (ret == -ENOENT) {
1198 if (dn.max_level == 0)
1199 return -ENOENT;
1200 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1201 dn.ofs_in_node, len);
1202 blkaddr += done;
1203 do_replace += done;
1204 goto next;
1205 }
1206
1207 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1208 dn.ofs_in_node, len);
1209 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1210 *blkaddr = f2fs_data_blkaddr(&dn);
1211
1212 if (__is_valid_data_blkaddr(*blkaddr) &&
1213 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1214 DATA_GENERIC_ENHANCE)) {
1215 f2fs_put_dnode(&dn);
1216 return -EFSCORRUPTED;
1217 }
1218
1219 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1220
1221 if (f2fs_lfs_mode(sbi)) {
1222 f2fs_put_dnode(&dn);
1223 return -EOPNOTSUPP;
1224 }
1225
1226 /* do not invalidate this block address */
1227 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1228 *do_replace = 1;
1229 }
1230 }
1231 f2fs_put_dnode(&dn);
1232next:
1233 len -= done;
1234 off += done;
1235 if (len)
1236 goto next_dnode;
1237 return 0;
1238}
1239
1240static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1241 int *do_replace, pgoff_t off, int len)
1242{
1243 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1244 struct dnode_of_data dn;
1245 int ret, i;
1246
1247 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1248 if (*do_replace == 0)
1249 continue;
1250
1251 set_new_dnode(&dn, inode, NULL, NULL, 0);
1252 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1253 if (ret) {
1254 dec_valid_block_count(sbi, inode, 1);
1255 f2fs_invalidate_blocks(sbi, *blkaddr);
1256 } else {
1257 f2fs_update_data_blkaddr(&dn, *blkaddr);
1258 }
1259 f2fs_put_dnode(&dn);
1260 }
1261 return 0;
1262}
1263
1264static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1265 block_t *blkaddr, int *do_replace,
1266 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1267{
1268 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1269 pgoff_t i = 0;
1270 int ret;
1271
1272 while (i < len) {
1273 if (blkaddr[i] == NULL_ADDR && !full) {
1274 i++;
1275 continue;
1276 }
1277
1278 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1279 struct dnode_of_data dn;
1280 struct node_info ni;
1281 size_t new_size;
1282 pgoff_t ilen;
1283
1284 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1285 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1286 if (ret)
1287 return ret;
1288
1289 ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
1290 if (ret) {
1291 f2fs_put_dnode(&dn);
1292 return ret;
1293 }
1294
1295 ilen = min((pgoff_t)
1296 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1297 dn.ofs_in_node, len - i);
1298 do {
1299 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1300 f2fs_truncate_data_blocks_range(&dn, 1);
1301
1302 if (do_replace[i]) {
1303 f2fs_i_blocks_write(src_inode,
1304 1, false, false);
1305 f2fs_i_blocks_write(dst_inode,
1306 1, true, false);
1307 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1308 blkaddr[i], ni.version, true, false);
1309
1310 do_replace[i] = 0;
1311 }
1312 dn.ofs_in_node++;
1313 i++;
1314 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1315 if (dst_inode->i_size < new_size)
1316 f2fs_i_size_write(dst_inode, new_size);
1317 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1318
1319 f2fs_put_dnode(&dn);
1320 } else {
1321 struct page *psrc, *pdst;
1322
1323 psrc = f2fs_get_lock_data_page(src_inode,
1324 src + i, true);
1325 if (IS_ERR(psrc))
1326 return PTR_ERR(psrc);
1327 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1328 true);
1329 if (IS_ERR(pdst)) {
1330 f2fs_put_page(psrc, 1);
1331 return PTR_ERR(pdst);
1332 }
1333
1334 f2fs_wait_on_page_writeback(pdst, DATA, true, true);
1335
1336 memcpy_page(pdst, 0, psrc, 0, PAGE_SIZE);
1337 set_page_dirty(pdst);
1338 set_page_private_gcing(pdst);
1339 f2fs_put_page(pdst, 1);
1340 f2fs_put_page(psrc, 1);
1341
1342 ret = f2fs_truncate_hole(src_inode,
1343 src + i, src + i + 1);
1344 if (ret)
1345 return ret;
1346 i++;
1347 }
1348 }
1349 return 0;
1350}
1351
1352static int __exchange_data_block(struct inode *src_inode,
1353 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1354 pgoff_t len, bool full)
1355{
1356 block_t *src_blkaddr;
1357 int *do_replace;
1358 pgoff_t olen;
1359 int ret;
1360
1361 while (len) {
1362 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1363
1364 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1365 array_size(olen, sizeof(block_t)),
1366 GFP_NOFS);
1367 if (!src_blkaddr)
1368 return -ENOMEM;
1369
1370 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1371 array_size(olen, sizeof(int)),
1372 GFP_NOFS);
1373 if (!do_replace) {
1374 kvfree(src_blkaddr);
1375 return -ENOMEM;
1376 }
1377
1378 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1379 do_replace, src, olen);
1380 if (ret)
1381 goto roll_back;
1382
1383 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1384 do_replace, src, dst, olen, full);
1385 if (ret)
1386 goto roll_back;
1387
1388 src += olen;
1389 dst += olen;
1390 len -= olen;
1391
1392 kvfree(src_blkaddr);
1393 kvfree(do_replace);
1394 }
1395 return 0;
1396
1397roll_back:
1398 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1399 kvfree(src_blkaddr);
1400 kvfree(do_replace);
1401 return ret;
1402}
1403
1404static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1405{
1406 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1407 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1408 pgoff_t start = offset >> PAGE_SHIFT;
1409 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1410 int ret;
1411
1412 f2fs_balance_fs(sbi, true);
1413
1414 /* avoid gc operation during block exchange */
1415 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1416 filemap_invalidate_lock(inode->i_mapping);
1417
1418 f2fs_lock_op(sbi);
1419 f2fs_drop_extent_tree(inode);
1420 truncate_pagecache(inode, offset);
1421 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1422 f2fs_unlock_op(sbi);
1423
1424 filemap_invalidate_unlock(inode->i_mapping);
1425 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1426 return ret;
1427}
1428
1429static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1430{
1431 loff_t new_size;
1432 int ret;
1433
1434 if (offset + len >= i_size_read(inode))
1435 return -EINVAL;
1436
1437 /* collapse range should be aligned to block size of f2fs. */
1438 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1439 return -EINVAL;
1440
1441 ret = f2fs_convert_inline_inode(inode);
1442 if (ret)
1443 return ret;
1444
1445 /* write out all dirty pages from offset */
1446 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1447 if (ret)
1448 return ret;
1449
1450 ret = f2fs_do_collapse(inode, offset, len);
1451 if (ret)
1452 return ret;
1453
1454 /* write out all moved pages, if possible */
1455 filemap_invalidate_lock(inode->i_mapping);
1456 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1457 truncate_pagecache(inode, offset);
1458
1459 new_size = i_size_read(inode) - len;
1460 ret = f2fs_truncate_blocks(inode, new_size, true);
1461 filemap_invalidate_unlock(inode->i_mapping);
1462 if (!ret)
1463 f2fs_i_size_write(inode, new_size);
1464 return ret;
1465}
1466
1467static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1468 pgoff_t end)
1469{
1470 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1471 pgoff_t index = start;
1472 unsigned int ofs_in_node = dn->ofs_in_node;
1473 blkcnt_t count = 0;
1474 int ret;
1475
1476 for (; index < end; index++, dn->ofs_in_node++) {
1477 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1478 count++;
1479 }
1480
1481 dn->ofs_in_node = ofs_in_node;
1482 ret = f2fs_reserve_new_blocks(dn, count);
1483 if (ret)
1484 return ret;
1485
1486 dn->ofs_in_node = ofs_in_node;
1487 for (index = start; index < end; index++, dn->ofs_in_node++) {
1488 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1489 /*
1490 * f2fs_reserve_new_blocks will not guarantee entire block
1491 * allocation.
1492 */
1493 if (dn->data_blkaddr == NULL_ADDR) {
1494 ret = -ENOSPC;
1495 break;
1496 }
1497
1498 if (dn->data_blkaddr == NEW_ADDR)
1499 continue;
1500
1501 if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
1502 DATA_GENERIC_ENHANCE)) {
1503 ret = -EFSCORRUPTED;
1504 break;
1505 }
1506
1507 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1508 f2fs_set_data_blkaddr(dn, NEW_ADDR);
1509 }
1510
1511 f2fs_update_read_extent_cache_range(dn, start, 0, index - start);
1512 f2fs_update_age_extent_cache_range(dn, start, index - start);
1513
1514 return ret;
1515}
1516
1517static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1518 int mode)
1519{
1520 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1521 struct address_space *mapping = inode->i_mapping;
1522 pgoff_t index, pg_start, pg_end;
1523 loff_t new_size = i_size_read(inode);
1524 loff_t off_start, off_end;
1525 int ret = 0;
1526
1527 ret = inode_newsize_ok(inode, (len + offset));
1528 if (ret)
1529 return ret;
1530
1531 ret = f2fs_convert_inline_inode(inode);
1532 if (ret)
1533 return ret;
1534
1535 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1536 if (ret)
1537 return ret;
1538
1539 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1540 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1541
1542 off_start = offset & (PAGE_SIZE - 1);
1543 off_end = (offset + len) & (PAGE_SIZE - 1);
1544
1545 if (pg_start == pg_end) {
1546 ret = fill_zero(inode, pg_start, off_start,
1547 off_end - off_start);
1548 if (ret)
1549 return ret;
1550
1551 new_size = max_t(loff_t, new_size, offset + len);
1552 } else {
1553 if (off_start) {
1554 ret = fill_zero(inode, pg_start++, off_start,
1555 PAGE_SIZE - off_start);
1556 if (ret)
1557 return ret;
1558
1559 new_size = max_t(loff_t, new_size,
1560 (loff_t)pg_start << PAGE_SHIFT);
1561 }
1562
1563 for (index = pg_start; index < pg_end;) {
1564 struct dnode_of_data dn;
1565 unsigned int end_offset;
1566 pgoff_t end;
1567
1568 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1569 filemap_invalidate_lock(mapping);
1570
1571 truncate_pagecache_range(inode,
1572 (loff_t)index << PAGE_SHIFT,
1573 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1574
1575 f2fs_lock_op(sbi);
1576
1577 set_new_dnode(&dn, inode, NULL, NULL, 0);
1578 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1579 if (ret) {
1580 f2fs_unlock_op(sbi);
1581 filemap_invalidate_unlock(mapping);
1582 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1583 goto out;
1584 }
1585
1586 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1587 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1588
1589 ret = f2fs_do_zero_range(&dn, index, end);
1590 f2fs_put_dnode(&dn);
1591
1592 f2fs_unlock_op(sbi);
1593 filemap_invalidate_unlock(mapping);
1594 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1595
1596 f2fs_balance_fs(sbi, dn.node_changed);
1597
1598 if (ret)
1599 goto out;
1600
1601 index = end;
1602 new_size = max_t(loff_t, new_size,
1603 (loff_t)index << PAGE_SHIFT);
1604 }
1605
1606 if (off_end) {
1607 ret = fill_zero(inode, pg_end, 0, off_end);
1608 if (ret)
1609 goto out;
1610
1611 new_size = max_t(loff_t, new_size, offset + len);
1612 }
1613 }
1614
1615out:
1616 if (new_size > i_size_read(inode)) {
1617 if (mode & FALLOC_FL_KEEP_SIZE)
1618 file_set_keep_isize(inode);
1619 else
1620 f2fs_i_size_write(inode, new_size);
1621 }
1622 return ret;
1623}
1624
1625static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1626{
1627 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1628 struct address_space *mapping = inode->i_mapping;
1629 pgoff_t nr, pg_start, pg_end, delta, idx;
1630 loff_t new_size;
1631 int ret = 0;
1632
1633 new_size = i_size_read(inode) + len;
1634 ret = inode_newsize_ok(inode, new_size);
1635 if (ret)
1636 return ret;
1637
1638 if (offset >= i_size_read(inode))
1639 return -EINVAL;
1640
1641 /* insert range should be aligned to block size of f2fs. */
1642 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1643 return -EINVAL;
1644
1645 ret = f2fs_convert_inline_inode(inode);
1646 if (ret)
1647 return ret;
1648
1649 f2fs_balance_fs(sbi, true);
1650
1651 filemap_invalidate_lock(mapping);
1652 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1653 filemap_invalidate_unlock(mapping);
1654 if (ret)
1655 return ret;
1656
1657 /* write out all dirty pages from offset */
1658 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1659 if (ret)
1660 return ret;
1661
1662 pg_start = offset >> PAGE_SHIFT;
1663 pg_end = (offset + len) >> PAGE_SHIFT;
1664 delta = pg_end - pg_start;
1665 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1666
1667 /* avoid gc operation during block exchange */
1668 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1669 filemap_invalidate_lock(mapping);
1670 truncate_pagecache(inode, offset);
1671
1672 while (!ret && idx > pg_start) {
1673 nr = idx - pg_start;
1674 if (nr > delta)
1675 nr = delta;
1676 idx -= nr;
1677
1678 f2fs_lock_op(sbi);
1679 f2fs_drop_extent_tree(inode);
1680
1681 ret = __exchange_data_block(inode, inode, idx,
1682 idx + delta, nr, false);
1683 f2fs_unlock_op(sbi);
1684 }
1685 filemap_invalidate_unlock(mapping);
1686 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1687 if (ret)
1688 return ret;
1689
1690 /* write out all moved pages, if possible */
1691 filemap_invalidate_lock(mapping);
1692 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1693 truncate_pagecache(inode, offset);
1694 filemap_invalidate_unlock(mapping);
1695
1696 if (!ret)
1697 f2fs_i_size_write(inode, new_size);
1698 return ret;
1699}
1700
1701static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
1702 loff_t len, int mode)
1703{
1704 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1705 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1706 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1707 .m_may_create = true };
1708 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1709 .init_gc_type = FG_GC,
1710 .should_migrate_blocks = false,
1711 .err_gc_skipped = true,
1712 .nr_free_secs = 0 };
1713 pgoff_t pg_start, pg_end;
1714 loff_t new_size;
1715 loff_t off_end;
1716 block_t expanded = 0;
1717 int err;
1718
1719 err = inode_newsize_ok(inode, (len + offset));
1720 if (err)
1721 return err;
1722
1723 err = f2fs_convert_inline_inode(inode);
1724 if (err)
1725 return err;
1726
1727 f2fs_balance_fs(sbi, true);
1728
1729 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1730 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1731 off_end = (offset + len) & (PAGE_SIZE - 1);
1732
1733 map.m_lblk = pg_start;
1734 map.m_len = pg_end - pg_start;
1735 if (off_end)
1736 map.m_len++;
1737
1738 if (!map.m_len)
1739 return 0;
1740
1741 if (f2fs_is_pinned_file(inode)) {
1742 block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
1743 block_t sec_len = roundup(map.m_len, sec_blks);
1744
1745 map.m_len = sec_blks;
1746next_alloc:
1747 if (has_not_enough_free_secs(sbi, 0,
1748 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1749 f2fs_down_write(&sbi->gc_lock);
1750 stat_inc_gc_call_count(sbi, FOREGROUND);
1751 err = f2fs_gc(sbi, &gc_control);
1752 if (err && err != -ENODATA)
1753 goto out_err;
1754 }
1755
1756 f2fs_down_write(&sbi->pin_sem);
1757
1758 err = f2fs_allocate_pinning_section(sbi);
1759 if (err) {
1760 f2fs_up_write(&sbi->pin_sem);
1761 goto out_err;
1762 }
1763
1764 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1765 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_DIO);
1766 file_dont_truncate(inode);
1767
1768 f2fs_up_write(&sbi->pin_sem);
1769
1770 expanded += map.m_len;
1771 sec_len -= map.m_len;
1772 map.m_lblk += map.m_len;
1773 if (!err && sec_len)
1774 goto next_alloc;
1775
1776 map.m_len = expanded;
1777 } else {
1778 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_AIO);
1779 expanded = map.m_len;
1780 }
1781out_err:
1782 if (err) {
1783 pgoff_t last_off;
1784
1785 if (!expanded)
1786 return err;
1787
1788 last_off = pg_start + expanded - 1;
1789
1790 /* update new size to the failed position */
1791 new_size = (last_off == pg_end) ? offset + len :
1792 (loff_t)(last_off + 1) << PAGE_SHIFT;
1793 } else {
1794 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1795 }
1796
1797 if (new_size > i_size_read(inode)) {
1798 if (mode & FALLOC_FL_KEEP_SIZE)
1799 file_set_keep_isize(inode);
1800 else
1801 f2fs_i_size_write(inode, new_size);
1802 }
1803
1804 return err;
1805}
1806
1807static long f2fs_fallocate(struct file *file, int mode,
1808 loff_t offset, loff_t len)
1809{
1810 struct inode *inode = file_inode(file);
1811 long ret = 0;
1812
1813 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1814 return -EIO;
1815 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1816 return -ENOSPC;
1817 if (!f2fs_is_compress_backend_ready(inode))
1818 return -EOPNOTSUPP;
1819
1820 /* f2fs only support ->fallocate for regular file */
1821 if (!S_ISREG(inode->i_mode))
1822 return -EINVAL;
1823
1824 if (IS_ENCRYPTED(inode) &&
1825 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1826 return -EOPNOTSUPP;
1827
1828 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1829 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1830 FALLOC_FL_INSERT_RANGE))
1831 return -EOPNOTSUPP;
1832
1833 inode_lock(inode);
1834
1835 /*
1836 * Pinned file should not support partial truncation since the block
1837 * can be used by applications.
1838 */
1839 if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
1840 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1841 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
1842 ret = -EOPNOTSUPP;
1843 goto out;
1844 }
1845
1846 ret = file_modified(file);
1847 if (ret)
1848 goto out;
1849
1850 if (mode & FALLOC_FL_PUNCH_HOLE) {
1851 if (offset >= inode->i_size)
1852 goto out;
1853
1854 ret = f2fs_punch_hole(inode, offset, len);
1855 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1856 ret = f2fs_collapse_range(inode, offset, len);
1857 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1858 ret = f2fs_zero_range(inode, offset, len, mode);
1859 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1860 ret = f2fs_insert_range(inode, offset, len);
1861 } else {
1862 ret = f2fs_expand_inode_data(inode, offset, len, mode);
1863 }
1864
1865 if (!ret) {
1866 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1867 f2fs_mark_inode_dirty_sync(inode, false);
1868 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1869 }
1870
1871out:
1872 inode_unlock(inode);
1873
1874 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1875 return ret;
1876}
1877
1878static int f2fs_release_file(struct inode *inode, struct file *filp)
1879{
1880 /*
1881 * f2fs_release_file is called at every close calls. So we should
1882 * not drop any inmemory pages by close called by other process.
1883 */
1884 if (!(filp->f_mode & FMODE_WRITE) ||
1885 atomic_read(&inode->i_writecount) != 1)
1886 return 0;
1887
1888 inode_lock(inode);
1889 f2fs_abort_atomic_write(inode, true);
1890 inode_unlock(inode);
1891
1892 return 0;
1893}
1894
1895static int f2fs_file_flush(struct file *file, fl_owner_t id)
1896{
1897 struct inode *inode = file_inode(file);
1898
1899 /*
1900 * If the process doing a transaction is crashed, we should do
1901 * roll-back. Otherwise, other reader/write can see corrupted database
1902 * until all the writers close its file. Since this should be done
1903 * before dropping file lock, it needs to do in ->flush.
1904 */
1905 if (F2FS_I(inode)->atomic_write_task == current &&
1906 (current->flags & PF_EXITING)) {
1907 inode_lock(inode);
1908 f2fs_abort_atomic_write(inode, true);
1909 inode_unlock(inode);
1910 }
1911
1912 return 0;
1913}
1914
1915static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1916{
1917 struct f2fs_inode_info *fi = F2FS_I(inode);
1918 u32 masked_flags = fi->i_flags & mask;
1919
1920 /* mask can be shrunk by flags_valid selector */
1921 iflags &= mask;
1922
1923 /* Is it quota file? Do not allow user to mess with it */
1924 if (IS_NOQUOTA(inode))
1925 return -EPERM;
1926
1927 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1928 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1929 return -EOPNOTSUPP;
1930 if (!f2fs_empty_dir(inode))
1931 return -ENOTEMPTY;
1932 }
1933
1934 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1935 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1936 return -EOPNOTSUPP;
1937 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1938 return -EINVAL;
1939 }
1940
1941 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1942 if (masked_flags & F2FS_COMPR_FL) {
1943 if (!f2fs_disable_compressed_file(inode))
1944 return -EINVAL;
1945 } else {
1946 /* try to convert inline_data to support compression */
1947 int err = f2fs_convert_inline_inode(inode);
1948 if (err)
1949 return err;
1950
1951 f2fs_down_write(&F2FS_I(inode)->i_sem);
1952 if (!f2fs_may_compress(inode) ||
1953 (S_ISREG(inode->i_mode) &&
1954 F2FS_HAS_BLOCKS(inode))) {
1955 f2fs_up_write(&F2FS_I(inode)->i_sem);
1956 return -EINVAL;
1957 }
1958 err = set_compress_context(inode);
1959 f2fs_up_write(&F2FS_I(inode)->i_sem);
1960
1961 if (err)
1962 return err;
1963 }
1964 }
1965
1966 fi->i_flags = iflags | (fi->i_flags & ~mask);
1967 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1968 (fi->i_flags & F2FS_NOCOMP_FL));
1969
1970 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1971 set_inode_flag(inode, FI_PROJ_INHERIT);
1972 else
1973 clear_inode_flag(inode, FI_PROJ_INHERIT);
1974
1975 inode_set_ctime_current(inode);
1976 f2fs_set_inode_flags(inode);
1977 f2fs_mark_inode_dirty_sync(inode, true);
1978 return 0;
1979}
1980
1981/* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
1982
1983/*
1984 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1985 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1986 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1987 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1988 *
1989 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
1990 * FS_IOC_FSSETXATTR is done by the VFS.
1991 */
1992
1993static const struct {
1994 u32 iflag;
1995 u32 fsflag;
1996} f2fs_fsflags_map[] = {
1997 { F2FS_COMPR_FL, FS_COMPR_FL },
1998 { F2FS_SYNC_FL, FS_SYNC_FL },
1999 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
2000 { F2FS_APPEND_FL, FS_APPEND_FL },
2001 { F2FS_NODUMP_FL, FS_NODUMP_FL },
2002 { F2FS_NOATIME_FL, FS_NOATIME_FL },
2003 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
2004 { F2FS_INDEX_FL, FS_INDEX_FL },
2005 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
2006 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
2007 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
2008};
2009
2010#define F2FS_GETTABLE_FS_FL ( \
2011 FS_COMPR_FL | \
2012 FS_SYNC_FL | \
2013 FS_IMMUTABLE_FL | \
2014 FS_APPEND_FL | \
2015 FS_NODUMP_FL | \
2016 FS_NOATIME_FL | \
2017 FS_NOCOMP_FL | \
2018 FS_INDEX_FL | \
2019 FS_DIRSYNC_FL | \
2020 FS_PROJINHERIT_FL | \
2021 FS_ENCRYPT_FL | \
2022 FS_INLINE_DATA_FL | \
2023 FS_NOCOW_FL | \
2024 FS_VERITY_FL | \
2025 FS_CASEFOLD_FL)
2026
2027#define F2FS_SETTABLE_FS_FL ( \
2028 FS_COMPR_FL | \
2029 FS_SYNC_FL | \
2030 FS_IMMUTABLE_FL | \
2031 FS_APPEND_FL | \
2032 FS_NODUMP_FL | \
2033 FS_NOATIME_FL | \
2034 FS_NOCOMP_FL | \
2035 FS_DIRSYNC_FL | \
2036 FS_PROJINHERIT_FL | \
2037 FS_CASEFOLD_FL)
2038
2039/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
2040static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
2041{
2042 u32 fsflags = 0;
2043 int i;
2044
2045 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2046 if (iflags & f2fs_fsflags_map[i].iflag)
2047 fsflags |= f2fs_fsflags_map[i].fsflag;
2048
2049 return fsflags;
2050}
2051
2052/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
2053static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
2054{
2055 u32 iflags = 0;
2056 int i;
2057
2058 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2059 if (fsflags & f2fs_fsflags_map[i].fsflag)
2060 iflags |= f2fs_fsflags_map[i].iflag;
2061
2062 return iflags;
2063}
2064
2065static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2066{
2067 struct inode *inode = file_inode(filp);
2068
2069 return put_user(inode->i_generation, (int __user *)arg);
2070}
2071
2072static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
2073{
2074 struct inode *inode = file_inode(filp);
2075 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2076 struct f2fs_inode_info *fi = F2FS_I(inode);
2077 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2078 struct inode *pinode;
2079 loff_t isize;
2080 int ret;
2081
2082 if (!inode_owner_or_capable(idmap, inode))
2083 return -EACCES;
2084
2085 if (!S_ISREG(inode->i_mode))
2086 return -EINVAL;
2087
2088 if (filp->f_flags & O_DIRECT)
2089 return -EINVAL;
2090
2091 ret = mnt_want_write_file(filp);
2092 if (ret)
2093 return ret;
2094
2095 inode_lock(inode);
2096
2097 if (!f2fs_disable_compressed_file(inode) ||
2098 f2fs_is_pinned_file(inode)) {
2099 ret = -EINVAL;
2100 goto out;
2101 }
2102
2103 if (f2fs_is_atomic_file(inode))
2104 goto out;
2105
2106 ret = f2fs_convert_inline_inode(inode);
2107 if (ret)
2108 goto out;
2109
2110 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
2111
2112 /*
2113 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2114 * f2fs_is_atomic_file.
2115 */
2116 if (get_dirty_pages(inode))
2117 f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2118 inode->i_ino, get_dirty_pages(inode));
2119 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2120 if (ret) {
2121 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2122 goto out;
2123 }
2124
2125 /* Check if the inode already has a COW inode */
2126 if (fi->cow_inode == NULL) {
2127 /* Create a COW inode for atomic write */
2128 pinode = f2fs_iget(inode->i_sb, fi->i_pino);
2129 if (IS_ERR(pinode)) {
2130 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2131 ret = PTR_ERR(pinode);
2132 goto out;
2133 }
2134
2135 ret = f2fs_get_tmpfile(idmap, pinode, &fi->cow_inode);
2136 iput(pinode);
2137 if (ret) {
2138 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2139 goto out;
2140 }
2141
2142 set_inode_flag(fi->cow_inode, FI_COW_FILE);
2143 clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
2144 } else {
2145 /* Reuse the already created COW inode */
2146 ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
2147 if (ret) {
2148 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2149 goto out;
2150 }
2151 }
2152
2153 f2fs_write_inode(inode, NULL);
2154
2155 stat_inc_atomic_inode(inode);
2156
2157 set_inode_flag(inode, FI_ATOMIC_FILE);
2158
2159 isize = i_size_read(inode);
2160 fi->original_i_size = isize;
2161 if (truncate) {
2162 set_inode_flag(inode, FI_ATOMIC_REPLACE);
2163 truncate_inode_pages_final(inode->i_mapping);
2164 f2fs_i_size_write(inode, 0);
2165 isize = 0;
2166 }
2167 f2fs_i_size_write(fi->cow_inode, isize);
2168
2169 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2170
2171 f2fs_update_time(sbi, REQ_TIME);
2172 fi->atomic_write_task = current;
2173 stat_update_max_atomic_write(inode);
2174 fi->atomic_write_cnt = 0;
2175out:
2176 inode_unlock(inode);
2177 mnt_drop_write_file(filp);
2178 return ret;
2179}
2180
2181static int f2fs_ioc_commit_atomic_write(struct file *filp)
2182{
2183 struct inode *inode = file_inode(filp);
2184 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2185 int ret;
2186
2187 if (!inode_owner_or_capable(idmap, inode))
2188 return -EACCES;
2189
2190 ret = mnt_want_write_file(filp);
2191 if (ret)
2192 return ret;
2193
2194 f2fs_balance_fs(F2FS_I_SB(inode), true);
2195
2196 inode_lock(inode);
2197
2198 if (f2fs_is_atomic_file(inode)) {
2199 ret = f2fs_commit_atomic_write(inode);
2200 if (!ret)
2201 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2202
2203 f2fs_abort_atomic_write(inode, ret);
2204 } else {
2205 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2206 }
2207
2208 inode_unlock(inode);
2209 mnt_drop_write_file(filp);
2210 return ret;
2211}
2212
2213static int f2fs_ioc_abort_atomic_write(struct file *filp)
2214{
2215 struct inode *inode = file_inode(filp);
2216 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2217 int ret;
2218
2219 if (!inode_owner_or_capable(idmap, inode))
2220 return -EACCES;
2221
2222 ret = mnt_want_write_file(filp);
2223 if (ret)
2224 return ret;
2225
2226 inode_lock(inode);
2227
2228 f2fs_abort_atomic_write(inode, true);
2229
2230 inode_unlock(inode);
2231
2232 mnt_drop_write_file(filp);
2233 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2234 return ret;
2235}
2236
2237static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2238{
2239 struct inode *inode = file_inode(filp);
2240 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2241 struct super_block *sb = sbi->sb;
2242 __u32 in;
2243 int ret = 0;
2244
2245 if (!capable(CAP_SYS_ADMIN))
2246 return -EPERM;
2247
2248 if (get_user(in, (__u32 __user *)arg))
2249 return -EFAULT;
2250
2251 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2252 ret = mnt_want_write_file(filp);
2253 if (ret) {
2254 if (ret == -EROFS) {
2255 ret = 0;
2256 f2fs_stop_checkpoint(sbi, false,
2257 STOP_CP_REASON_SHUTDOWN);
2258 trace_f2fs_shutdown(sbi, in, ret);
2259 }
2260 return ret;
2261 }
2262 }
2263
2264 switch (in) {
2265 case F2FS_GOING_DOWN_FULLSYNC:
2266 ret = bdev_freeze(sb->s_bdev);
2267 if (ret)
2268 goto out;
2269 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2270 bdev_thaw(sb->s_bdev);
2271 break;
2272 case F2FS_GOING_DOWN_METASYNC:
2273 /* do checkpoint only */
2274 ret = f2fs_sync_fs(sb, 1);
2275 if (ret) {
2276 if (ret == -EIO)
2277 ret = 0;
2278 goto out;
2279 }
2280 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2281 break;
2282 case F2FS_GOING_DOWN_NOSYNC:
2283 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2284 break;
2285 case F2FS_GOING_DOWN_METAFLUSH:
2286 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2287 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2288 break;
2289 case F2FS_GOING_DOWN_NEED_FSCK:
2290 set_sbi_flag(sbi, SBI_NEED_FSCK);
2291 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2292 set_sbi_flag(sbi, SBI_IS_DIRTY);
2293 /* do checkpoint only */
2294 ret = f2fs_sync_fs(sb, 1);
2295 if (ret == -EIO)
2296 ret = 0;
2297 goto out;
2298 default:
2299 ret = -EINVAL;
2300 goto out;
2301 }
2302
2303 f2fs_stop_gc_thread(sbi);
2304 f2fs_stop_discard_thread(sbi);
2305
2306 f2fs_drop_discard_cmd(sbi);
2307 clear_opt(sbi, DISCARD);
2308
2309 f2fs_update_time(sbi, REQ_TIME);
2310out:
2311 if (in != F2FS_GOING_DOWN_FULLSYNC)
2312 mnt_drop_write_file(filp);
2313
2314 trace_f2fs_shutdown(sbi, in, ret);
2315
2316 return ret;
2317}
2318
2319static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2320{
2321 struct inode *inode = file_inode(filp);
2322 struct super_block *sb = inode->i_sb;
2323 struct fstrim_range range;
2324 int ret;
2325
2326 if (!capable(CAP_SYS_ADMIN))
2327 return -EPERM;
2328
2329 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2330 return -EOPNOTSUPP;
2331
2332 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2333 sizeof(range)))
2334 return -EFAULT;
2335
2336 ret = mnt_want_write_file(filp);
2337 if (ret)
2338 return ret;
2339
2340 range.minlen = max((unsigned int)range.minlen,
2341 bdev_discard_granularity(sb->s_bdev));
2342 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2343 mnt_drop_write_file(filp);
2344 if (ret < 0)
2345 return ret;
2346
2347 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2348 sizeof(range)))
2349 return -EFAULT;
2350 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2351 return 0;
2352}
2353
2354static bool uuid_is_nonzero(__u8 u[16])
2355{
2356 int i;
2357
2358 for (i = 0; i < 16; i++)
2359 if (u[i])
2360 return true;
2361 return false;
2362}
2363
2364static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2365{
2366 struct inode *inode = file_inode(filp);
2367
2368 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2369 return -EOPNOTSUPP;
2370
2371 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2372
2373 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2374}
2375
2376static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2377{
2378 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2379 return -EOPNOTSUPP;
2380 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2381}
2382
2383static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2384{
2385 struct inode *inode = file_inode(filp);
2386 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2387 u8 encrypt_pw_salt[16];
2388 int err;
2389
2390 if (!f2fs_sb_has_encrypt(sbi))
2391 return -EOPNOTSUPP;
2392
2393 err = mnt_want_write_file(filp);
2394 if (err)
2395 return err;
2396
2397 f2fs_down_write(&sbi->sb_lock);
2398
2399 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2400 goto got_it;
2401
2402 /* update superblock with uuid */
2403 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2404
2405 err = f2fs_commit_super(sbi, false);
2406 if (err) {
2407 /* undo new data */
2408 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2409 goto out_err;
2410 }
2411got_it:
2412 memcpy(encrypt_pw_salt, sbi->raw_super->encrypt_pw_salt, 16);
2413out_err:
2414 f2fs_up_write(&sbi->sb_lock);
2415 mnt_drop_write_file(filp);
2416
2417 if (!err && copy_to_user((__u8 __user *)arg, encrypt_pw_salt, 16))
2418 err = -EFAULT;
2419
2420 return err;
2421}
2422
2423static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2424 unsigned long arg)
2425{
2426 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2427 return -EOPNOTSUPP;
2428
2429 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2430}
2431
2432static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2433{
2434 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2435 return -EOPNOTSUPP;
2436
2437 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2438}
2439
2440static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2441{
2442 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2443 return -EOPNOTSUPP;
2444
2445 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2446}
2447
2448static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2449 unsigned long arg)
2450{
2451 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2452 return -EOPNOTSUPP;
2453
2454 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2455}
2456
2457static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2458 unsigned long arg)
2459{
2460 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2461 return -EOPNOTSUPP;
2462
2463 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2464}
2465
2466static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2467{
2468 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2469 return -EOPNOTSUPP;
2470
2471 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2472}
2473
2474static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2475{
2476 struct inode *inode = file_inode(filp);
2477 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2478 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
2479 .no_bg_gc = false,
2480 .should_migrate_blocks = false,
2481 .nr_free_secs = 0 };
2482 __u32 sync;
2483 int ret;
2484
2485 if (!capable(CAP_SYS_ADMIN))
2486 return -EPERM;
2487
2488 if (get_user(sync, (__u32 __user *)arg))
2489 return -EFAULT;
2490
2491 if (f2fs_readonly(sbi->sb))
2492 return -EROFS;
2493
2494 ret = mnt_want_write_file(filp);
2495 if (ret)
2496 return ret;
2497
2498 if (!sync) {
2499 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2500 ret = -EBUSY;
2501 goto out;
2502 }
2503 } else {
2504 f2fs_down_write(&sbi->gc_lock);
2505 }
2506
2507 gc_control.init_gc_type = sync ? FG_GC : BG_GC;
2508 gc_control.err_gc_skipped = sync;
2509 stat_inc_gc_call_count(sbi, FOREGROUND);
2510 ret = f2fs_gc(sbi, &gc_control);
2511out:
2512 mnt_drop_write_file(filp);
2513 return ret;
2514}
2515
2516static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2517{
2518 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2519 struct f2fs_gc_control gc_control = {
2520 .init_gc_type = range->sync ? FG_GC : BG_GC,
2521 .no_bg_gc = false,
2522 .should_migrate_blocks = false,
2523 .err_gc_skipped = range->sync,
2524 .nr_free_secs = 0 };
2525 u64 end;
2526 int ret;
2527
2528 if (!capable(CAP_SYS_ADMIN))
2529 return -EPERM;
2530 if (f2fs_readonly(sbi->sb))
2531 return -EROFS;
2532
2533 end = range->start + range->len;
2534 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2535 end >= MAX_BLKADDR(sbi))
2536 return -EINVAL;
2537
2538 ret = mnt_want_write_file(filp);
2539 if (ret)
2540 return ret;
2541
2542do_more:
2543 if (!range->sync) {
2544 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2545 ret = -EBUSY;
2546 goto out;
2547 }
2548 } else {
2549 f2fs_down_write(&sbi->gc_lock);
2550 }
2551
2552 gc_control.victim_segno = GET_SEGNO(sbi, range->start);
2553 stat_inc_gc_call_count(sbi, FOREGROUND);
2554 ret = f2fs_gc(sbi, &gc_control);
2555 if (ret) {
2556 if (ret == -EBUSY)
2557 ret = -EAGAIN;
2558 goto out;
2559 }
2560 range->start += CAP_BLKS_PER_SEC(sbi);
2561 if (range->start <= end)
2562 goto do_more;
2563out:
2564 mnt_drop_write_file(filp);
2565 return ret;
2566}
2567
2568static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2569{
2570 struct f2fs_gc_range range;
2571
2572 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2573 sizeof(range)))
2574 return -EFAULT;
2575 return __f2fs_ioc_gc_range(filp, &range);
2576}
2577
2578static int f2fs_ioc_write_checkpoint(struct file *filp)
2579{
2580 struct inode *inode = file_inode(filp);
2581 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2582 int ret;
2583
2584 if (!capable(CAP_SYS_ADMIN))
2585 return -EPERM;
2586
2587 if (f2fs_readonly(sbi->sb))
2588 return -EROFS;
2589
2590 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2591 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2592 return -EINVAL;
2593 }
2594
2595 ret = mnt_want_write_file(filp);
2596 if (ret)
2597 return ret;
2598
2599 ret = f2fs_sync_fs(sbi->sb, 1);
2600
2601 mnt_drop_write_file(filp);
2602 return ret;
2603}
2604
2605static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2606 struct file *filp,
2607 struct f2fs_defragment *range)
2608{
2609 struct inode *inode = file_inode(filp);
2610 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2611 .m_seg_type = NO_CHECK_TYPE,
2612 .m_may_create = false };
2613 struct extent_info ei = {};
2614 pgoff_t pg_start, pg_end, next_pgofs;
2615 unsigned int total = 0, sec_num;
2616 block_t blk_end = 0;
2617 bool fragmented = false;
2618 int err;
2619
2620 pg_start = range->start >> PAGE_SHIFT;
2621 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2622
2623 f2fs_balance_fs(sbi, true);
2624
2625 inode_lock(inode);
2626
2627 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
2628 err = -EINVAL;
2629 goto unlock_out;
2630 }
2631
2632 /* if in-place-update policy is enabled, don't waste time here */
2633 set_inode_flag(inode, FI_OPU_WRITE);
2634 if (f2fs_should_update_inplace(inode, NULL)) {
2635 err = -EINVAL;
2636 goto out;
2637 }
2638
2639 /* writeback all dirty pages in the range */
2640 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2641 range->start + range->len - 1);
2642 if (err)
2643 goto out;
2644
2645 /*
2646 * lookup mapping info in extent cache, skip defragmenting if physical
2647 * block addresses are continuous.
2648 */
2649 if (f2fs_lookup_read_extent_cache(inode, pg_start, &ei)) {
2650 if (ei.fofs + ei.len >= pg_end)
2651 goto out;
2652 }
2653
2654 map.m_lblk = pg_start;
2655 map.m_next_pgofs = &next_pgofs;
2656
2657 /*
2658 * lookup mapping info in dnode page cache, skip defragmenting if all
2659 * physical block addresses are continuous even if there are hole(s)
2660 * in logical blocks.
2661 */
2662 while (map.m_lblk < pg_end) {
2663 map.m_len = pg_end - map.m_lblk;
2664 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2665 if (err)
2666 goto out;
2667
2668 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2669 map.m_lblk = next_pgofs;
2670 continue;
2671 }
2672
2673 if (blk_end && blk_end != map.m_pblk)
2674 fragmented = true;
2675
2676 /* record total count of block that we're going to move */
2677 total += map.m_len;
2678
2679 blk_end = map.m_pblk + map.m_len;
2680
2681 map.m_lblk += map.m_len;
2682 }
2683
2684 if (!fragmented) {
2685 total = 0;
2686 goto out;
2687 }
2688
2689 sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
2690
2691 /*
2692 * make sure there are enough free section for LFS allocation, this can
2693 * avoid defragment running in SSR mode when free section are allocated
2694 * intensively
2695 */
2696 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2697 err = -EAGAIN;
2698 goto out;
2699 }
2700
2701 map.m_lblk = pg_start;
2702 map.m_len = pg_end - pg_start;
2703 total = 0;
2704
2705 while (map.m_lblk < pg_end) {
2706 pgoff_t idx;
2707 int cnt = 0;
2708
2709do_map:
2710 map.m_len = pg_end - map.m_lblk;
2711 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2712 if (err)
2713 goto clear_out;
2714
2715 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2716 map.m_lblk = next_pgofs;
2717 goto check;
2718 }
2719
2720 set_inode_flag(inode, FI_SKIP_WRITES);
2721
2722 idx = map.m_lblk;
2723 while (idx < map.m_lblk + map.m_len &&
2724 cnt < BLKS_PER_SEG(sbi)) {
2725 struct page *page;
2726
2727 page = f2fs_get_lock_data_page(inode, idx, true);
2728 if (IS_ERR(page)) {
2729 err = PTR_ERR(page);
2730 goto clear_out;
2731 }
2732
2733 set_page_dirty(page);
2734 set_page_private_gcing(page);
2735 f2fs_put_page(page, 1);
2736
2737 idx++;
2738 cnt++;
2739 total++;
2740 }
2741
2742 map.m_lblk = idx;
2743check:
2744 if (map.m_lblk < pg_end && cnt < BLKS_PER_SEG(sbi))
2745 goto do_map;
2746
2747 clear_inode_flag(inode, FI_SKIP_WRITES);
2748
2749 err = filemap_fdatawrite(inode->i_mapping);
2750 if (err)
2751 goto out;
2752 }
2753clear_out:
2754 clear_inode_flag(inode, FI_SKIP_WRITES);
2755out:
2756 clear_inode_flag(inode, FI_OPU_WRITE);
2757unlock_out:
2758 inode_unlock(inode);
2759 if (!err)
2760 range->len = (u64)total << PAGE_SHIFT;
2761 return err;
2762}
2763
2764static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2765{
2766 struct inode *inode = file_inode(filp);
2767 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2768 struct f2fs_defragment range;
2769 int err;
2770
2771 if (!capable(CAP_SYS_ADMIN))
2772 return -EPERM;
2773
2774 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2775 return -EINVAL;
2776
2777 if (f2fs_readonly(sbi->sb))
2778 return -EROFS;
2779
2780 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2781 sizeof(range)))
2782 return -EFAULT;
2783
2784 /* verify alignment of offset & size */
2785 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2786 return -EINVAL;
2787
2788 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2789 max_file_blocks(inode)))
2790 return -EINVAL;
2791
2792 err = mnt_want_write_file(filp);
2793 if (err)
2794 return err;
2795
2796 err = f2fs_defragment_range(sbi, filp, &range);
2797 mnt_drop_write_file(filp);
2798
2799 f2fs_update_time(sbi, REQ_TIME);
2800 if (err < 0)
2801 return err;
2802
2803 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2804 sizeof(range)))
2805 return -EFAULT;
2806
2807 return 0;
2808}
2809
2810static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2811 struct file *file_out, loff_t pos_out, size_t len)
2812{
2813 struct inode *src = file_inode(file_in);
2814 struct inode *dst = file_inode(file_out);
2815 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2816 size_t olen = len, dst_max_i_size = 0;
2817 size_t dst_osize;
2818 int ret;
2819
2820 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2821 src->i_sb != dst->i_sb)
2822 return -EXDEV;
2823
2824 if (unlikely(f2fs_readonly(src->i_sb)))
2825 return -EROFS;
2826
2827 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2828 return -EINVAL;
2829
2830 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2831 return -EOPNOTSUPP;
2832
2833 if (pos_out < 0 || pos_in < 0)
2834 return -EINVAL;
2835
2836 if (src == dst) {
2837 if (pos_in == pos_out)
2838 return 0;
2839 if (pos_out > pos_in && pos_out < pos_in + len)
2840 return -EINVAL;
2841 }
2842
2843 inode_lock(src);
2844 if (src != dst) {
2845 ret = -EBUSY;
2846 if (!inode_trylock(dst))
2847 goto out;
2848 }
2849
2850 if (f2fs_compressed_file(src) || f2fs_compressed_file(dst) ||
2851 f2fs_is_pinned_file(src) || f2fs_is_pinned_file(dst)) {
2852 ret = -EOPNOTSUPP;
2853 goto out_unlock;
2854 }
2855
2856 ret = -EINVAL;
2857 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2858 goto out_unlock;
2859 if (len == 0)
2860 olen = len = src->i_size - pos_in;
2861 if (pos_in + len == src->i_size)
2862 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2863 if (len == 0) {
2864 ret = 0;
2865 goto out_unlock;
2866 }
2867
2868 dst_osize = dst->i_size;
2869 if (pos_out + olen > dst->i_size)
2870 dst_max_i_size = pos_out + olen;
2871
2872 /* verify the end result is block aligned */
2873 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2874 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2875 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2876 goto out_unlock;
2877
2878 ret = f2fs_convert_inline_inode(src);
2879 if (ret)
2880 goto out_unlock;
2881
2882 ret = f2fs_convert_inline_inode(dst);
2883 if (ret)
2884 goto out_unlock;
2885
2886 /* write out all dirty pages from offset */
2887 ret = filemap_write_and_wait_range(src->i_mapping,
2888 pos_in, pos_in + len);
2889 if (ret)
2890 goto out_unlock;
2891
2892 ret = filemap_write_and_wait_range(dst->i_mapping,
2893 pos_out, pos_out + len);
2894 if (ret)
2895 goto out_unlock;
2896
2897 f2fs_balance_fs(sbi, true);
2898
2899 f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2900 if (src != dst) {
2901 ret = -EBUSY;
2902 if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2903 goto out_src;
2904 }
2905
2906 f2fs_lock_op(sbi);
2907 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2908 pos_out >> F2FS_BLKSIZE_BITS,
2909 len >> F2FS_BLKSIZE_BITS, false);
2910
2911 if (!ret) {
2912 if (dst_max_i_size)
2913 f2fs_i_size_write(dst, dst_max_i_size);
2914 else if (dst_osize != dst->i_size)
2915 f2fs_i_size_write(dst, dst_osize);
2916 }
2917 f2fs_unlock_op(sbi);
2918
2919 if (src != dst)
2920 f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2921out_src:
2922 f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2923 if (ret)
2924 goto out_unlock;
2925
2926 inode_set_mtime_to_ts(src, inode_set_ctime_current(src));
2927 f2fs_mark_inode_dirty_sync(src, false);
2928 if (src != dst) {
2929 inode_set_mtime_to_ts(dst, inode_set_ctime_current(dst));
2930 f2fs_mark_inode_dirty_sync(dst, false);
2931 }
2932 f2fs_update_time(sbi, REQ_TIME);
2933
2934out_unlock:
2935 if (src != dst)
2936 inode_unlock(dst);
2937out:
2938 inode_unlock(src);
2939 return ret;
2940}
2941
2942static int __f2fs_ioc_move_range(struct file *filp,
2943 struct f2fs_move_range *range)
2944{
2945 struct fd dst;
2946 int err;
2947
2948 if (!(filp->f_mode & FMODE_READ) ||
2949 !(filp->f_mode & FMODE_WRITE))
2950 return -EBADF;
2951
2952 dst = fdget(range->dst_fd);
2953 if (!dst.file)
2954 return -EBADF;
2955
2956 if (!(dst.file->f_mode & FMODE_WRITE)) {
2957 err = -EBADF;
2958 goto err_out;
2959 }
2960
2961 err = mnt_want_write_file(filp);
2962 if (err)
2963 goto err_out;
2964
2965 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2966 range->pos_out, range->len);
2967
2968 mnt_drop_write_file(filp);
2969err_out:
2970 fdput(dst);
2971 return err;
2972}
2973
2974static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2975{
2976 struct f2fs_move_range range;
2977
2978 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2979 sizeof(range)))
2980 return -EFAULT;
2981 return __f2fs_ioc_move_range(filp, &range);
2982}
2983
2984static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2985{
2986 struct inode *inode = file_inode(filp);
2987 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2988 struct sit_info *sm = SIT_I(sbi);
2989 unsigned int start_segno = 0, end_segno = 0;
2990 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2991 struct f2fs_flush_device range;
2992 struct f2fs_gc_control gc_control = {
2993 .init_gc_type = FG_GC,
2994 .should_migrate_blocks = true,
2995 .err_gc_skipped = true,
2996 .nr_free_secs = 0 };
2997 int ret;
2998
2999 if (!capable(CAP_SYS_ADMIN))
3000 return -EPERM;
3001
3002 if (f2fs_readonly(sbi->sb))
3003 return -EROFS;
3004
3005 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
3006 return -EINVAL;
3007
3008 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
3009 sizeof(range)))
3010 return -EFAULT;
3011
3012 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
3013 __is_large_section(sbi)) {
3014 f2fs_warn(sbi, "Can't flush %u in %d for SEGS_PER_SEC %u != 1",
3015 range.dev_num, sbi->s_ndevs, SEGS_PER_SEC(sbi));
3016 return -EINVAL;
3017 }
3018
3019 ret = mnt_want_write_file(filp);
3020 if (ret)
3021 return ret;
3022
3023 if (range.dev_num != 0)
3024 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
3025 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
3026
3027 start_segno = sm->last_victim[FLUSH_DEVICE];
3028 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
3029 start_segno = dev_start_segno;
3030 end_segno = min(start_segno + range.segments, dev_end_segno);
3031
3032 while (start_segno < end_segno) {
3033 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
3034 ret = -EBUSY;
3035 goto out;
3036 }
3037 sm->last_victim[GC_CB] = end_segno + 1;
3038 sm->last_victim[GC_GREEDY] = end_segno + 1;
3039 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
3040
3041 gc_control.victim_segno = start_segno;
3042 stat_inc_gc_call_count(sbi, FOREGROUND);
3043 ret = f2fs_gc(sbi, &gc_control);
3044 if (ret == -EAGAIN)
3045 ret = 0;
3046 else if (ret < 0)
3047 break;
3048 start_segno++;
3049 }
3050out:
3051 mnt_drop_write_file(filp);
3052 return ret;
3053}
3054
3055static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
3056{
3057 struct inode *inode = file_inode(filp);
3058 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
3059
3060 /* Must validate to set it with SQLite behavior in Android. */
3061 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
3062
3063 return put_user(sb_feature, (u32 __user *)arg);
3064}
3065
3066#ifdef CONFIG_QUOTA
3067int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3068{
3069 struct dquot *transfer_to[MAXQUOTAS] = {};
3070 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3071 struct super_block *sb = sbi->sb;
3072 int err;
3073
3074 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3075 if (IS_ERR(transfer_to[PRJQUOTA]))
3076 return PTR_ERR(transfer_to[PRJQUOTA]);
3077
3078 err = __dquot_transfer(inode, transfer_to);
3079 if (err)
3080 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3081 dqput(transfer_to[PRJQUOTA]);
3082 return err;
3083}
3084
3085static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3086{
3087 struct f2fs_inode_info *fi = F2FS_I(inode);
3088 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3089 struct f2fs_inode *ri = NULL;
3090 kprojid_t kprojid;
3091 int err;
3092
3093 if (!f2fs_sb_has_project_quota(sbi)) {
3094 if (projid != F2FS_DEF_PROJID)
3095 return -EOPNOTSUPP;
3096 else
3097 return 0;
3098 }
3099
3100 if (!f2fs_has_extra_attr(inode))
3101 return -EOPNOTSUPP;
3102
3103 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3104
3105 if (projid_eq(kprojid, fi->i_projid))
3106 return 0;
3107
3108 err = -EPERM;
3109 /* Is it quota file? Do not allow user to mess with it */
3110 if (IS_NOQUOTA(inode))
3111 return err;
3112
3113 if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
3114 return -EOVERFLOW;
3115
3116 err = f2fs_dquot_initialize(inode);
3117 if (err)
3118 return err;
3119
3120 f2fs_lock_op(sbi);
3121 err = f2fs_transfer_project_quota(inode, kprojid);
3122 if (err)
3123 goto out_unlock;
3124
3125 fi->i_projid = kprojid;
3126 inode_set_ctime_current(inode);
3127 f2fs_mark_inode_dirty_sync(inode, true);
3128out_unlock:
3129 f2fs_unlock_op(sbi);
3130 return err;
3131}
3132#else
3133int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3134{
3135 return 0;
3136}
3137
3138static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3139{
3140 if (projid != F2FS_DEF_PROJID)
3141 return -EOPNOTSUPP;
3142 return 0;
3143}
3144#endif
3145
3146int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3147{
3148 struct inode *inode = d_inode(dentry);
3149 struct f2fs_inode_info *fi = F2FS_I(inode);
3150 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3151
3152 if (IS_ENCRYPTED(inode))
3153 fsflags |= FS_ENCRYPT_FL;
3154 if (IS_VERITY(inode))
3155 fsflags |= FS_VERITY_FL;
3156 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3157 fsflags |= FS_INLINE_DATA_FL;
3158 if (is_inode_flag_set(inode, FI_PIN_FILE))
3159 fsflags |= FS_NOCOW_FL;
3160
3161 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3162
3163 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3164 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3165
3166 return 0;
3167}
3168
3169int f2fs_fileattr_set(struct mnt_idmap *idmap,
3170 struct dentry *dentry, struct fileattr *fa)
3171{
3172 struct inode *inode = d_inode(dentry);
3173 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3174 u32 iflags;
3175 int err;
3176
3177 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3178 return -EIO;
3179 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3180 return -ENOSPC;
3181 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3182 return -EOPNOTSUPP;
3183 fsflags &= F2FS_SETTABLE_FS_FL;
3184 if (!fa->flags_valid)
3185 mask &= FS_COMMON_FL;
3186
3187 iflags = f2fs_fsflags_to_iflags(fsflags);
3188 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3189 return -EOPNOTSUPP;
3190
3191 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3192 if (!err)
3193 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3194
3195 return err;
3196}
3197
3198int f2fs_pin_file_control(struct inode *inode, bool inc)
3199{
3200 struct f2fs_inode_info *fi = F2FS_I(inode);
3201 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3202
3203 /* Use i_gc_failures for normal file as a risk signal. */
3204 if (inc)
3205 f2fs_i_gc_failures_write(inode,
3206 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3207
3208 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3209 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3210 __func__, inode->i_ino,
3211 fi->i_gc_failures[GC_FAILURE_PIN]);
3212 clear_inode_flag(inode, FI_PIN_FILE);
3213 return -EAGAIN;
3214 }
3215 return 0;
3216}
3217
3218static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3219{
3220 struct inode *inode = file_inode(filp);
3221 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3222 __u32 pin;
3223 int ret = 0;
3224
3225 if (get_user(pin, (__u32 __user *)arg))
3226 return -EFAULT;
3227
3228 if (!S_ISREG(inode->i_mode))
3229 return -EINVAL;
3230
3231 if (f2fs_readonly(sbi->sb))
3232 return -EROFS;
3233
3234 ret = mnt_want_write_file(filp);
3235 if (ret)
3236 return ret;
3237
3238 inode_lock(inode);
3239
3240 if (!pin) {
3241 clear_inode_flag(inode, FI_PIN_FILE);
3242 f2fs_i_gc_failures_write(inode, 0);
3243 goto done;
3244 } else if (f2fs_is_pinned_file(inode)) {
3245 goto done;
3246 }
3247
3248 if (f2fs_sb_has_blkzoned(sbi) && F2FS_HAS_BLOCKS(inode)) {
3249 ret = -EFBIG;
3250 goto out;
3251 }
3252
3253 /* Let's allow file pinning on zoned device. */
3254 if (!f2fs_sb_has_blkzoned(sbi) &&
3255 f2fs_should_update_outplace(inode, NULL)) {
3256 ret = -EINVAL;
3257 goto out;
3258 }
3259
3260 if (f2fs_pin_file_control(inode, false)) {
3261 ret = -EAGAIN;
3262 goto out;
3263 }
3264
3265 ret = f2fs_convert_inline_inode(inode);
3266 if (ret)
3267 goto out;
3268
3269 if (!f2fs_disable_compressed_file(inode)) {
3270 ret = -EOPNOTSUPP;
3271 goto out;
3272 }
3273
3274 set_inode_flag(inode, FI_PIN_FILE);
3275 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3276done:
3277 f2fs_update_time(sbi, REQ_TIME);
3278out:
3279 inode_unlock(inode);
3280 mnt_drop_write_file(filp);
3281 return ret;
3282}
3283
3284static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3285{
3286 struct inode *inode = file_inode(filp);
3287 __u32 pin = 0;
3288
3289 if (is_inode_flag_set(inode, FI_PIN_FILE))
3290 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3291 return put_user(pin, (u32 __user *)arg);
3292}
3293
3294int f2fs_precache_extents(struct inode *inode)
3295{
3296 struct f2fs_inode_info *fi = F2FS_I(inode);
3297 struct f2fs_map_blocks map;
3298 pgoff_t m_next_extent;
3299 loff_t end;
3300 int err;
3301
3302 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3303 return -EOPNOTSUPP;
3304
3305 map.m_lblk = 0;
3306 map.m_pblk = 0;
3307 map.m_next_pgofs = NULL;
3308 map.m_next_extent = &m_next_extent;
3309 map.m_seg_type = NO_CHECK_TYPE;
3310 map.m_may_create = false;
3311 end = F2FS_BLK_ALIGN(i_size_read(inode));
3312
3313 while (map.m_lblk < end) {
3314 map.m_len = end - map.m_lblk;
3315
3316 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3317 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRECACHE);
3318 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3319 if (err || !map.m_len)
3320 return err;
3321
3322 map.m_lblk = m_next_extent;
3323 }
3324
3325 return 0;
3326}
3327
3328static int f2fs_ioc_precache_extents(struct file *filp)
3329{
3330 return f2fs_precache_extents(file_inode(filp));
3331}
3332
3333static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3334{
3335 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3336 __u64 block_count;
3337
3338 if (!capable(CAP_SYS_ADMIN))
3339 return -EPERM;
3340
3341 if (f2fs_readonly(sbi->sb))
3342 return -EROFS;
3343
3344 if (copy_from_user(&block_count, (void __user *)arg,
3345 sizeof(block_count)))
3346 return -EFAULT;
3347
3348 return f2fs_resize_fs(filp, block_count);
3349}
3350
3351static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3352{
3353 struct inode *inode = file_inode(filp);
3354
3355 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3356
3357 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3358 f2fs_warn(F2FS_I_SB(inode),
3359 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem",
3360 inode->i_ino);
3361 return -EOPNOTSUPP;
3362 }
3363
3364 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3365}
3366
3367static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3368{
3369 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3370 return -EOPNOTSUPP;
3371
3372 return fsverity_ioctl_measure(filp, (void __user *)arg);
3373}
3374
3375static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3376{
3377 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3378 return -EOPNOTSUPP;
3379
3380 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3381}
3382
3383static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3384{
3385 struct inode *inode = file_inode(filp);
3386 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3387 char *vbuf;
3388 int count;
3389 int err = 0;
3390
3391 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3392 if (!vbuf)
3393 return -ENOMEM;
3394
3395 f2fs_down_read(&sbi->sb_lock);
3396 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3397 ARRAY_SIZE(sbi->raw_super->volume_name),
3398 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3399 f2fs_up_read(&sbi->sb_lock);
3400
3401 if (copy_to_user((char __user *)arg, vbuf,
3402 min(FSLABEL_MAX, count)))
3403 err = -EFAULT;
3404
3405 kfree(vbuf);
3406 return err;
3407}
3408
3409static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3410{
3411 struct inode *inode = file_inode(filp);
3412 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3413 char *vbuf;
3414 int err = 0;
3415
3416 if (!capable(CAP_SYS_ADMIN))
3417 return -EPERM;
3418
3419 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3420 if (IS_ERR(vbuf))
3421 return PTR_ERR(vbuf);
3422
3423 err = mnt_want_write_file(filp);
3424 if (err)
3425 goto out;
3426
3427 f2fs_down_write(&sbi->sb_lock);
3428
3429 memset(sbi->raw_super->volume_name, 0,
3430 sizeof(sbi->raw_super->volume_name));
3431 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3432 sbi->raw_super->volume_name,
3433 ARRAY_SIZE(sbi->raw_super->volume_name));
3434
3435 err = f2fs_commit_super(sbi, false);
3436
3437 f2fs_up_write(&sbi->sb_lock);
3438
3439 mnt_drop_write_file(filp);
3440out:
3441 kfree(vbuf);
3442 return err;
3443}
3444
3445static int f2fs_get_compress_blocks(struct inode *inode, __u64 *blocks)
3446{
3447 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3448 return -EOPNOTSUPP;
3449
3450 if (!f2fs_compressed_file(inode))
3451 return -EINVAL;
3452
3453 *blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3454
3455 return 0;
3456}
3457
3458static int f2fs_ioc_get_compress_blocks(struct file *filp, unsigned long arg)
3459{
3460 struct inode *inode = file_inode(filp);
3461 __u64 blocks;
3462 int ret;
3463
3464 ret = f2fs_get_compress_blocks(inode, &blocks);
3465 if (ret < 0)
3466 return ret;
3467
3468 return put_user(blocks, (u64 __user *)arg);
3469}
3470
3471static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3472{
3473 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3474 unsigned int released_blocks = 0;
3475 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3476 block_t blkaddr;
3477 int i;
3478
3479 for (i = 0; i < count; i++) {
3480 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3481 dn->ofs_in_node + i);
3482
3483 if (!__is_valid_data_blkaddr(blkaddr))
3484 continue;
3485 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3486 DATA_GENERIC_ENHANCE)))
3487 return -EFSCORRUPTED;
3488 }
3489
3490 while (count) {
3491 int compr_blocks = 0;
3492
3493 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3494 blkaddr = f2fs_data_blkaddr(dn);
3495
3496 if (i == 0) {
3497 if (blkaddr == COMPRESS_ADDR)
3498 continue;
3499 dn->ofs_in_node += cluster_size;
3500 goto next;
3501 }
3502
3503 if (__is_valid_data_blkaddr(blkaddr))
3504 compr_blocks++;
3505
3506 if (blkaddr != NEW_ADDR)
3507 continue;
3508
3509 f2fs_set_data_blkaddr(dn, NULL_ADDR);
3510 }
3511
3512 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3513 dec_valid_block_count(sbi, dn->inode,
3514 cluster_size - compr_blocks);
3515
3516 released_blocks += cluster_size - compr_blocks;
3517next:
3518 count -= cluster_size;
3519 }
3520
3521 return released_blocks;
3522}
3523
3524static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3525{
3526 struct inode *inode = file_inode(filp);
3527 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3528 pgoff_t page_idx = 0, last_idx;
3529 unsigned int released_blocks = 0;
3530 int ret;
3531 int writecount;
3532
3533 if (!f2fs_sb_has_compression(sbi))
3534 return -EOPNOTSUPP;
3535
3536 if (f2fs_readonly(sbi->sb))
3537 return -EROFS;
3538
3539 ret = mnt_want_write_file(filp);
3540 if (ret)
3541 return ret;
3542
3543 f2fs_balance_fs(sbi, true);
3544
3545 inode_lock(inode);
3546
3547 writecount = atomic_read(&inode->i_writecount);
3548 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3549 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3550 ret = -EBUSY;
3551 goto out;
3552 }
3553
3554 if (!f2fs_compressed_file(inode) ||
3555 is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3556 ret = -EINVAL;
3557 goto out;
3558 }
3559
3560 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3561 if (ret)
3562 goto out;
3563
3564 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3565 ret = -EPERM;
3566 goto out;
3567 }
3568
3569 set_inode_flag(inode, FI_COMPRESS_RELEASED);
3570 inode_set_ctime_current(inode);
3571 f2fs_mark_inode_dirty_sync(inode, true);
3572
3573 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3574 filemap_invalidate_lock(inode->i_mapping);
3575
3576 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3577
3578 while (page_idx < last_idx) {
3579 struct dnode_of_data dn;
3580 pgoff_t end_offset, count;
3581
3582 f2fs_lock_op(sbi);
3583
3584 set_new_dnode(&dn, inode, NULL, NULL, 0);
3585 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3586 if (ret) {
3587 f2fs_unlock_op(sbi);
3588 if (ret == -ENOENT) {
3589 page_idx = f2fs_get_next_page_offset(&dn,
3590 page_idx);
3591 ret = 0;
3592 continue;
3593 }
3594 break;
3595 }
3596
3597 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3598 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3599 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3600
3601 ret = release_compress_blocks(&dn, count);
3602
3603 f2fs_put_dnode(&dn);
3604
3605 f2fs_unlock_op(sbi);
3606
3607 if (ret < 0)
3608 break;
3609
3610 page_idx += count;
3611 released_blocks += ret;
3612 }
3613
3614 filemap_invalidate_unlock(inode->i_mapping);
3615 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3616out:
3617 inode_unlock(inode);
3618
3619 mnt_drop_write_file(filp);
3620
3621 if (ret >= 0) {
3622 ret = put_user(released_blocks, (u64 __user *)arg);
3623 } else if (released_blocks &&
3624 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3625 set_sbi_flag(sbi, SBI_NEED_FSCK);
3626 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3627 "iblocks=%llu, released=%u, compr_blocks=%u, "
3628 "run fsck to fix.",
3629 __func__, inode->i_ino, inode->i_blocks,
3630 released_blocks,
3631 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3632 }
3633
3634 return ret;
3635}
3636
3637static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count,
3638 unsigned int *reserved_blocks)
3639{
3640 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3641 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3642 block_t blkaddr;
3643 int i;
3644
3645 for (i = 0; i < count; i++) {
3646 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3647 dn->ofs_in_node + i);
3648
3649 if (!__is_valid_data_blkaddr(blkaddr))
3650 continue;
3651 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3652 DATA_GENERIC_ENHANCE)))
3653 return -EFSCORRUPTED;
3654 }
3655
3656 while (count) {
3657 int compr_blocks = 0;
3658 blkcnt_t reserved = 0;
3659 blkcnt_t to_reserved;
3660 int ret;
3661
3662 for (i = 0; i < cluster_size; i++) {
3663 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3664 dn->ofs_in_node + i);
3665
3666 if (i == 0) {
3667 if (blkaddr != COMPRESS_ADDR) {
3668 dn->ofs_in_node += cluster_size;
3669 goto next;
3670 }
3671 continue;
3672 }
3673
3674 /*
3675 * compressed cluster was not released due to it
3676 * fails in release_compress_blocks(), so NEW_ADDR
3677 * is a possible case.
3678 */
3679 if (blkaddr == NEW_ADDR) {
3680 reserved++;
3681 continue;
3682 }
3683 if (__is_valid_data_blkaddr(blkaddr)) {
3684 compr_blocks++;
3685 continue;
3686 }
3687 }
3688
3689 to_reserved = cluster_size - compr_blocks - reserved;
3690
3691 /* for the case all blocks in cluster were reserved */
3692 if (to_reserved == 1) {
3693 dn->ofs_in_node += cluster_size;
3694 goto next;
3695 }
3696
3697 ret = inc_valid_block_count(sbi, dn->inode,
3698 &to_reserved, false);
3699 if (unlikely(ret))
3700 return ret;
3701
3702 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3703 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
3704 f2fs_set_data_blkaddr(dn, NEW_ADDR);
3705 }
3706
3707 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3708
3709 *reserved_blocks += to_reserved;
3710next:
3711 count -= cluster_size;
3712 }
3713
3714 return 0;
3715}
3716
3717static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3718{
3719 struct inode *inode = file_inode(filp);
3720 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3721 pgoff_t page_idx = 0, last_idx;
3722 unsigned int reserved_blocks = 0;
3723 int ret;
3724
3725 if (!f2fs_sb_has_compression(sbi))
3726 return -EOPNOTSUPP;
3727
3728 if (f2fs_readonly(sbi->sb))
3729 return -EROFS;
3730
3731 ret = mnt_want_write_file(filp);
3732 if (ret)
3733 return ret;
3734
3735 f2fs_balance_fs(sbi, true);
3736
3737 inode_lock(inode);
3738
3739 if (!f2fs_compressed_file(inode) ||
3740 !is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3741 ret = -EINVAL;
3742 goto unlock_inode;
3743 }
3744
3745 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3746 goto unlock_inode;
3747
3748 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3749 filemap_invalidate_lock(inode->i_mapping);
3750
3751 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3752
3753 while (page_idx < last_idx) {
3754 struct dnode_of_data dn;
3755 pgoff_t end_offset, count;
3756
3757 f2fs_lock_op(sbi);
3758
3759 set_new_dnode(&dn, inode, NULL, NULL, 0);
3760 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3761 if (ret) {
3762 f2fs_unlock_op(sbi);
3763 if (ret == -ENOENT) {
3764 page_idx = f2fs_get_next_page_offset(&dn,
3765 page_idx);
3766 ret = 0;
3767 continue;
3768 }
3769 break;
3770 }
3771
3772 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3773 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3774 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3775
3776 ret = reserve_compress_blocks(&dn, count, &reserved_blocks);
3777
3778 f2fs_put_dnode(&dn);
3779
3780 f2fs_unlock_op(sbi);
3781
3782 if (ret < 0)
3783 break;
3784
3785 page_idx += count;
3786 }
3787
3788 filemap_invalidate_unlock(inode->i_mapping);
3789 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3790
3791 if (!ret) {
3792 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
3793 inode_set_ctime_current(inode);
3794 f2fs_mark_inode_dirty_sync(inode, true);
3795 }
3796unlock_inode:
3797 inode_unlock(inode);
3798 mnt_drop_write_file(filp);
3799
3800 if (!ret) {
3801 ret = put_user(reserved_blocks, (u64 __user *)arg);
3802 } else if (reserved_blocks &&
3803 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3804 set_sbi_flag(sbi, SBI_NEED_FSCK);
3805 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3806 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3807 "run fsck to fix.",
3808 __func__, inode->i_ino, inode->i_blocks,
3809 reserved_blocks,
3810 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3811 }
3812
3813 return ret;
3814}
3815
3816static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3817 pgoff_t off, block_t block, block_t len, u32 flags)
3818{
3819 sector_t sector = SECTOR_FROM_BLOCK(block);
3820 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3821 int ret = 0;
3822
3823 if (flags & F2FS_TRIM_FILE_DISCARD) {
3824 if (bdev_max_secure_erase_sectors(bdev))
3825 ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
3826 GFP_NOFS);
3827 else
3828 ret = blkdev_issue_discard(bdev, sector, nr_sects,
3829 GFP_NOFS);
3830 }
3831
3832 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3833 if (IS_ENCRYPTED(inode))
3834 ret = fscrypt_zeroout_range(inode, off, block, len);
3835 else
3836 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3837 GFP_NOFS, 0);
3838 }
3839
3840 return ret;
3841}
3842
3843static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3844{
3845 struct inode *inode = file_inode(filp);
3846 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3847 struct address_space *mapping = inode->i_mapping;
3848 struct block_device *prev_bdev = NULL;
3849 struct f2fs_sectrim_range range;
3850 pgoff_t index, pg_end, prev_index = 0;
3851 block_t prev_block = 0, len = 0;
3852 loff_t end_addr;
3853 bool to_end = false;
3854 int ret = 0;
3855
3856 if (!(filp->f_mode & FMODE_WRITE))
3857 return -EBADF;
3858
3859 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3860 sizeof(range)))
3861 return -EFAULT;
3862
3863 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3864 !S_ISREG(inode->i_mode))
3865 return -EINVAL;
3866
3867 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3868 !f2fs_hw_support_discard(sbi)) ||
3869 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3870 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3871 return -EOPNOTSUPP;
3872
3873 file_start_write(filp);
3874 inode_lock(inode);
3875
3876 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3877 range.start >= inode->i_size) {
3878 ret = -EINVAL;
3879 goto err;
3880 }
3881
3882 if (range.len == 0)
3883 goto err;
3884
3885 if (inode->i_size - range.start > range.len) {
3886 end_addr = range.start + range.len;
3887 } else {
3888 end_addr = range.len == (u64)-1 ?
3889 sbi->sb->s_maxbytes : inode->i_size;
3890 to_end = true;
3891 }
3892
3893 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3894 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3895 ret = -EINVAL;
3896 goto err;
3897 }
3898
3899 index = F2FS_BYTES_TO_BLK(range.start);
3900 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3901
3902 ret = f2fs_convert_inline_inode(inode);
3903 if (ret)
3904 goto err;
3905
3906 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3907 filemap_invalidate_lock(mapping);
3908
3909 ret = filemap_write_and_wait_range(mapping, range.start,
3910 to_end ? LLONG_MAX : end_addr - 1);
3911 if (ret)
3912 goto out;
3913
3914 truncate_inode_pages_range(mapping, range.start,
3915 to_end ? -1 : end_addr - 1);
3916
3917 while (index < pg_end) {
3918 struct dnode_of_data dn;
3919 pgoff_t end_offset, count;
3920 int i;
3921
3922 set_new_dnode(&dn, inode, NULL, NULL, 0);
3923 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3924 if (ret) {
3925 if (ret == -ENOENT) {
3926 index = f2fs_get_next_page_offset(&dn, index);
3927 continue;
3928 }
3929 goto out;
3930 }
3931
3932 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3933 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3934 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3935 struct block_device *cur_bdev;
3936 block_t blkaddr = f2fs_data_blkaddr(&dn);
3937
3938 if (!__is_valid_data_blkaddr(blkaddr))
3939 continue;
3940
3941 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3942 DATA_GENERIC_ENHANCE)) {
3943 ret = -EFSCORRUPTED;
3944 f2fs_put_dnode(&dn);
3945 goto out;
3946 }
3947
3948 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3949 if (f2fs_is_multi_device(sbi)) {
3950 int di = f2fs_target_device_index(sbi, blkaddr);
3951
3952 blkaddr -= FDEV(di).start_blk;
3953 }
3954
3955 if (len) {
3956 if (prev_bdev == cur_bdev &&
3957 index == prev_index + len &&
3958 blkaddr == prev_block + len) {
3959 len++;
3960 } else {
3961 ret = f2fs_secure_erase(prev_bdev,
3962 inode, prev_index, prev_block,
3963 len, range.flags);
3964 if (ret) {
3965 f2fs_put_dnode(&dn);
3966 goto out;
3967 }
3968
3969 len = 0;
3970 }
3971 }
3972
3973 if (!len) {
3974 prev_bdev = cur_bdev;
3975 prev_index = index;
3976 prev_block = blkaddr;
3977 len = 1;
3978 }
3979 }
3980
3981 f2fs_put_dnode(&dn);
3982
3983 if (fatal_signal_pending(current)) {
3984 ret = -EINTR;
3985 goto out;
3986 }
3987 cond_resched();
3988 }
3989
3990 if (len)
3991 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3992 prev_block, len, range.flags);
3993out:
3994 filemap_invalidate_unlock(mapping);
3995 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3996err:
3997 inode_unlock(inode);
3998 file_end_write(filp);
3999
4000 return ret;
4001}
4002
4003static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
4004{
4005 struct inode *inode = file_inode(filp);
4006 struct f2fs_comp_option option;
4007
4008 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
4009 return -EOPNOTSUPP;
4010
4011 inode_lock_shared(inode);
4012
4013 if (!f2fs_compressed_file(inode)) {
4014 inode_unlock_shared(inode);
4015 return -ENODATA;
4016 }
4017
4018 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
4019 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
4020
4021 inode_unlock_shared(inode);
4022
4023 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
4024 sizeof(option)))
4025 return -EFAULT;
4026
4027 return 0;
4028}
4029
4030static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
4031{
4032 struct inode *inode = file_inode(filp);
4033 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4034 struct f2fs_comp_option option;
4035 int ret = 0;
4036
4037 if (!f2fs_sb_has_compression(sbi))
4038 return -EOPNOTSUPP;
4039
4040 if (!(filp->f_mode & FMODE_WRITE))
4041 return -EBADF;
4042
4043 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
4044 sizeof(option)))
4045 return -EFAULT;
4046
4047 if (option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
4048 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
4049 option.algorithm >= COMPRESS_MAX)
4050 return -EINVAL;
4051
4052 file_start_write(filp);
4053 inode_lock(inode);
4054
4055 f2fs_down_write(&F2FS_I(inode)->i_sem);
4056 if (!f2fs_compressed_file(inode)) {
4057 ret = -EINVAL;
4058 goto out;
4059 }
4060
4061 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
4062 ret = -EBUSY;
4063 goto out;
4064 }
4065
4066 if (F2FS_HAS_BLOCKS(inode)) {
4067 ret = -EFBIG;
4068 goto out;
4069 }
4070
4071 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
4072 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
4073 F2FS_I(inode)->i_cluster_size = BIT(option.log_cluster_size);
4074 /* Set default level */
4075 if (F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD)
4076 F2FS_I(inode)->i_compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
4077 else
4078 F2FS_I(inode)->i_compress_level = 0;
4079 /* Adjust mount option level */
4080 if (option.algorithm == F2FS_OPTION(sbi).compress_algorithm &&
4081 F2FS_OPTION(sbi).compress_level)
4082 F2FS_I(inode)->i_compress_level = F2FS_OPTION(sbi).compress_level;
4083 f2fs_mark_inode_dirty_sync(inode, true);
4084
4085 if (!f2fs_is_compress_backend_ready(inode))
4086 f2fs_warn(sbi, "compression algorithm is successfully set, "
4087 "but current kernel doesn't support this algorithm.");
4088out:
4089 f2fs_up_write(&F2FS_I(inode)->i_sem);
4090 inode_unlock(inode);
4091 file_end_write(filp);
4092
4093 return ret;
4094}
4095
4096static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
4097{
4098 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
4099 struct address_space *mapping = inode->i_mapping;
4100 struct page *page;
4101 pgoff_t redirty_idx = page_idx;
4102 int i, page_len = 0, ret = 0;
4103
4104 page_cache_ra_unbounded(&ractl, len, 0);
4105
4106 for (i = 0; i < len; i++, page_idx++) {
4107 page = read_cache_page(mapping, page_idx, NULL, NULL);
4108 if (IS_ERR(page)) {
4109 ret = PTR_ERR(page);
4110 break;
4111 }
4112 page_len++;
4113 }
4114
4115 for (i = 0; i < page_len; i++, redirty_idx++) {
4116 page = find_lock_page(mapping, redirty_idx);
4117
4118 /* It will never fail, when page has pinned above */
4119 f2fs_bug_on(F2FS_I_SB(inode), !page);
4120
4121 set_page_dirty(page);
4122 set_page_private_gcing(page);
4123 f2fs_put_page(page, 1);
4124 f2fs_put_page(page, 0);
4125 }
4126
4127 return ret;
4128}
4129
4130static int f2fs_ioc_decompress_file(struct file *filp)
4131{
4132 struct inode *inode = file_inode(filp);
4133 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4134 struct f2fs_inode_info *fi = F2FS_I(inode);
4135 pgoff_t page_idx = 0, last_idx;
4136 int cluster_size = fi->i_cluster_size;
4137 int count, ret;
4138
4139 if (!f2fs_sb_has_compression(sbi) ||
4140 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4141 return -EOPNOTSUPP;
4142
4143 if (!(filp->f_mode & FMODE_WRITE))
4144 return -EBADF;
4145
4146 f2fs_balance_fs(sbi, true);
4147
4148 file_start_write(filp);
4149 inode_lock(inode);
4150
4151 if (!f2fs_is_compress_backend_ready(inode)) {
4152 ret = -EOPNOTSUPP;
4153 goto out;
4154 }
4155
4156 if (!f2fs_compressed_file(inode) ||
4157 is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4158 ret = -EINVAL;
4159 goto out;
4160 }
4161
4162 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4163 if (ret)
4164 goto out;
4165
4166 if (!atomic_read(&fi->i_compr_blocks))
4167 goto out;
4168
4169 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4170
4171 count = last_idx - page_idx;
4172 while (count && count >= cluster_size) {
4173 ret = redirty_blocks(inode, page_idx, cluster_size);
4174 if (ret < 0)
4175 break;
4176
4177 if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
4178 ret = filemap_fdatawrite(inode->i_mapping);
4179 if (ret < 0)
4180 break;
4181 }
4182
4183 count -= cluster_size;
4184 page_idx += cluster_size;
4185
4186 cond_resched();
4187 if (fatal_signal_pending(current)) {
4188 ret = -EINTR;
4189 break;
4190 }
4191 }
4192
4193 if (!ret)
4194 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4195 LLONG_MAX);
4196
4197 if (ret)
4198 f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
4199 __func__, ret);
4200out:
4201 inode_unlock(inode);
4202 file_end_write(filp);
4203
4204 return ret;
4205}
4206
4207static int f2fs_ioc_compress_file(struct file *filp)
4208{
4209 struct inode *inode = file_inode(filp);
4210 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4211 pgoff_t page_idx = 0, last_idx;
4212 int cluster_size = F2FS_I(inode)->i_cluster_size;
4213 int count, ret;
4214
4215 if (!f2fs_sb_has_compression(sbi) ||
4216 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4217 return -EOPNOTSUPP;
4218
4219 if (!(filp->f_mode & FMODE_WRITE))
4220 return -EBADF;
4221
4222 f2fs_balance_fs(sbi, true);
4223
4224 file_start_write(filp);
4225 inode_lock(inode);
4226
4227 if (!f2fs_is_compress_backend_ready(inode)) {
4228 ret = -EOPNOTSUPP;
4229 goto out;
4230 }
4231
4232 if (!f2fs_compressed_file(inode) ||
4233 is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4234 ret = -EINVAL;
4235 goto out;
4236 }
4237
4238 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4239 if (ret)
4240 goto out;
4241
4242 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4243
4244 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4245
4246 count = last_idx - page_idx;
4247 while (count && count >= cluster_size) {
4248 ret = redirty_blocks(inode, page_idx, cluster_size);
4249 if (ret < 0)
4250 break;
4251
4252 if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
4253 ret = filemap_fdatawrite(inode->i_mapping);
4254 if (ret < 0)
4255 break;
4256 }
4257
4258 count -= cluster_size;
4259 page_idx += cluster_size;
4260
4261 cond_resched();
4262 if (fatal_signal_pending(current)) {
4263 ret = -EINTR;
4264 break;
4265 }
4266 }
4267
4268 if (!ret)
4269 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4270 LLONG_MAX);
4271
4272 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4273
4274 if (ret)
4275 f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
4276 __func__, ret);
4277out:
4278 inode_unlock(inode);
4279 file_end_write(filp);
4280
4281 return ret;
4282}
4283
4284static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4285{
4286 switch (cmd) {
4287 case FS_IOC_GETVERSION:
4288 return f2fs_ioc_getversion(filp, arg);
4289 case F2FS_IOC_START_ATOMIC_WRITE:
4290 return f2fs_ioc_start_atomic_write(filp, false);
4291 case F2FS_IOC_START_ATOMIC_REPLACE:
4292 return f2fs_ioc_start_atomic_write(filp, true);
4293 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4294 return f2fs_ioc_commit_atomic_write(filp);
4295 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4296 return f2fs_ioc_abort_atomic_write(filp);
4297 case F2FS_IOC_START_VOLATILE_WRITE:
4298 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4299 return -EOPNOTSUPP;
4300 case F2FS_IOC_SHUTDOWN:
4301 return f2fs_ioc_shutdown(filp, arg);
4302 case FITRIM:
4303 return f2fs_ioc_fitrim(filp, arg);
4304 case FS_IOC_SET_ENCRYPTION_POLICY:
4305 return f2fs_ioc_set_encryption_policy(filp, arg);
4306 case FS_IOC_GET_ENCRYPTION_POLICY:
4307 return f2fs_ioc_get_encryption_policy(filp, arg);
4308 case FS_IOC_GET_ENCRYPTION_PWSALT:
4309 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4310 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4311 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4312 case FS_IOC_ADD_ENCRYPTION_KEY:
4313 return f2fs_ioc_add_encryption_key(filp, arg);
4314 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4315 return f2fs_ioc_remove_encryption_key(filp, arg);
4316 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4317 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4318 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4319 return f2fs_ioc_get_encryption_key_status(filp, arg);
4320 case FS_IOC_GET_ENCRYPTION_NONCE:
4321 return f2fs_ioc_get_encryption_nonce(filp, arg);
4322 case F2FS_IOC_GARBAGE_COLLECT:
4323 return f2fs_ioc_gc(filp, arg);
4324 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4325 return f2fs_ioc_gc_range(filp, arg);
4326 case F2FS_IOC_WRITE_CHECKPOINT:
4327 return f2fs_ioc_write_checkpoint(filp);
4328 case F2FS_IOC_DEFRAGMENT:
4329 return f2fs_ioc_defragment(filp, arg);
4330 case F2FS_IOC_MOVE_RANGE:
4331 return f2fs_ioc_move_range(filp, arg);
4332 case F2FS_IOC_FLUSH_DEVICE:
4333 return f2fs_ioc_flush_device(filp, arg);
4334 case F2FS_IOC_GET_FEATURES:
4335 return f2fs_ioc_get_features(filp, arg);
4336 case F2FS_IOC_GET_PIN_FILE:
4337 return f2fs_ioc_get_pin_file(filp, arg);
4338 case F2FS_IOC_SET_PIN_FILE:
4339 return f2fs_ioc_set_pin_file(filp, arg);
4340 case F2FS_IOC_PRECACHE_EXTENTS:
4341 return f2fs_ioc_precache_extents(filp);
4342 case F2FS_IOC_RESIZE_FS:
4343 return f2fs_ioc_resize_fs(filp, arg);
4344 case FS_IOC_ENABLE_VERITY:
4345 return f2fs_ioc_enable_verity(filp, arg);
4346 case FS_IOC_MEASURE_VERITY:
4347 return f2fs_ioc_measure_verity(filp, arg);
4348 case FS_IOC_READ_VERITY_METADATA:
4349 return f2fs_ioc_read_verity_metadata(filp, arg);
4350 case FS_IOC_GETFSLABEL:
4351 return f2fs_ioc_getfslabel(filp, arg);
4352 case FS_IOC_SETFSLABEL:
4353 return f2fs_ioc_setfslabel(filp, arg);
4354 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4355 return f2fs_ioc_get_compress_blocks(filp, arg);
4356 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4357 return f2fs_release_compress_blocks(filp, arg);
4358 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4359 return f2fs_reserve_compress_blocks(filp, arg);
4360 case F2FS_IOC_SEC_TRIM_FILE:
4361 return f2fs_sec_trim_file(filp, arg);
4362 case F2FS_IOC_GET_COMPRESS_OPTION:
4363 return f2fs_ioc_get_compress_option(filp, arg);
4364 case F2FS_IOC_SET_COMPRESS_OPTION:
4365 return f2fs_ioc_set_compress_option(filp, arg);
4366 case F2FS_IOC_DECOMPRESS_FILE:
4367 return f2fs_ioc_decompress_file(filp);
4368 case F2FS_IOC_COMPRESS_FILE:
4369 return f2fs_ioc_compress_file(filp);
4370 default:
4371 return -ENOTTY;
4372 }
4373}
4374
4375long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4376{
4377 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4378 return -EIO;
4379 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4380 return -ENOSPC;
4381
4382 return __f2fs_ioctl(filp, cmd, arg);
4383}
4384
4385/*
4386 * Return %true if the given read or write request should use direct I/O, or
4387 * %false if it should use buffered I/O.
4388 */
4389static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
4390 struct iov_iter *iter)
4391{
4392 unsigned int align;
4393
4394 if (!(iocb->ki_flags & IOCB_DIRECT))
4395 return false;
4396
4397 if (f2fs_force_buffered_io(inode, iov_iter_rw(iter)))
4398 return false;
4399
4400 /*
4401 * Direct I/O not aligned to the disk's logical_block_size will be
4402 * attempted, but will fail with -EINVAL.
4403 *
4404 * f2fs additionally requires that direct I/O be aligned to the
4405 * filesystem block size, which is often a stricter requirement.
4406 * However, f2fs traditionally falls back to buffered I/O on requests
4407 * that are logical_block_size-aligned but not fs-block aligned.
4408 *
4409 * The below logic implements this behavior.
4410 */
4411 align = iocb->ki_pos | iov_iter_alignment(iter);
4412 if (!IS_ALIGNED(align, i_blocksize(inode)) &&
4413 IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
4414 return false;
4415
4416 return true;
4417}
4418
4419static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
4420 unsigned int flags)
4421{
4422 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4423
4424 dec_page_count(sbi, F2FS_DIO_READ);
4425 if (error)
4426 return error;
4427 f2fs_update_iostat(sbi, NULL, APP_DIRECT_READ_IO, size);
4428 return 0;
4429}
4430
4431static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
4432 .end_io = f2fs_dio_read_end_io,
4433};
4434
4435static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
4436{
4437 struct file *file = iocb->ki_filp;
4438 struct inode *inode = file_inode(file);
4439 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4440 struct f2fs_inode_info *fi = F2FS_I(inode);
4441 const loff_t pos = iocb->ki_pos;
4442 const size_t count = iov_iter_count(to);
4443 struct iomap_dio *dio;
4444 ssize_t ret;
4445
4446 if (count == 0)
4447 return 0; /* skip atime update */
4448
4449 trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
4450
4451 if (iocb->ki_flags & IOCB_NOWAIT) {
4452 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4453 ret = -EAGAIN;
4454 goto out;
4455 }
4456 } else {
4457 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4458 }
4459
4460 /*
4461 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4462 * the higher-level function iomap_dio_rw() in order to ensure that the
4463 * F2FS_DIO_READ counter will be decremented correctly in all cases.
4464 */
4465 inc_page_count(sbi, F2FS_DIO_READ);
4466 dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
4467 &f2fs_iomap_dio_read_ops, 0, NULL, 0);
4468 if (IS_ERR_OR_NULL(dio)) {
4469 ret = PTR_ERR_OR_ZERO(dio);
4470 if (ret != -EIOCBQUEUED)
4471 dec_page_count(sbi, F2FS_DIO_READ);
4472 } else {
4473 ret = iomap_dio_complete(dio);
4474 }
4475
4476 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4477
4478 file_accessed(file);
4479out:
4480 trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
4481 return ret;
4482}
4483
4484static void f2fs_trace_rw_file_path(struct file *file, loff_t pos, size_t count,
4485 int rw)
4486{
4487 struct inode *inode = file_inode(file);
4488 char *buf, *path;
4489
4490 buf = f2fs_getname(F2FS_I_SB(inode));
4491 if (!buf)
4492 return;
4493 path = dentry_path_raw(file_dentry(file), buf, PATH_MAX);
4494 if (IS_ERR(path))
4495 goto free_buf;
4496 if (rw == WRITE)
4497 trace_f2fs_datawrite_start(inode, pos, count,
4498 current->pid, path, current->comm);
4499 else
4500 trace_f2fs_dataread_start(inode, pos, count,
4501 current->pid, path, current->comm);
4502free_buf:
4503 f2fs_putname(buf);
4504}
4505
4506static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
4507{
4508 struct inode *inode = file_inode(iocb->ki_filp);
4509 const loff_t pos = iocb->ki_pos;
4510 ssize_t ret;
4511
4512 if (!f2fs_is_compress_backend_ready(inode))
4513 return -EOPNOTSUPP;
4514
4515 if (trace_f2fs_dataread_start_enabled())
4516 f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
4517 iov_iter_count(to), READ);
4518
4519 if (f2fs_should_use_dio(inode, iocb, to)) {
4520 ret = f2fs_dio_read_iter(iocb, to);
4521 } else {
4522 ret = filemap_read(iocb, to, 0);
4523 if (ret > 0)
4524 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4525 APP_BUFFERED_READ_IO, ret);
4526 }
4527 if (trace_f2fs_dataread_end_enabled())
4528 trace_f2fs_dataread_end(inode, pos, ret);
4529 return ret;
4530}
4531
4532static ssize_t f2fs_file_splice_read(struct file *in, loff_t *ppos,
4533 struct pipe_inode_info *pipe,
4534 size_t len, unsigned int flags)
4535{
4536 struct inode *inode = file_inode(in);
4537 const loff_t pos = *ppos;
4538 ssize_t ret;
4539
4540 if (!f2fs_is_compress_backend_ready(inode))
4541 return -EOPNOTSUPP;
4542
4543 if (trace_f2fs_dataread_start_enabled())
4544 f2fs_trace_rw_file_path(in, pos, len, READ);
4545
4546 ret = filemap_splice_read(in, ppos, pipe, len, flags);
4547 if (ret > 0)
4548 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4549 APP_BUFFERED_READ_IO, ret);
4550
4551 if (trace_f2fs_dataread_end_enabled())
4552 trace_f2fs_dataread_end(inode, pos, ret);
4553 return ret;
4554}
4555
4556static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
4557{
4558 struct file *file = iocb->ki_filp;
4559 struct inode *inode = file_inode(file);
4560 ssize_t count;
4561 int err;
4562
4563 if (IS_IMMUTABLE(inode))
4564 return -EPERM;
4565
4566 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
4567 return -EPERM;
4568
4569 count = generic_write_checks(iocb, from);
4570 if (count <= 0)
4571 return count;
4572
4573 err = file_modified(file);
4574 if (err)
4575 return err;
4576 return count;
4577}
4578
4579/*
4580 * Preallocate blocks for a write request, if it is possible and helpful to do
4581 * so. Returns a positive number if blocks may have been preallocated, 0 if no
4582 * blocks were preallocated, or a negative errno value if something went
4583 * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
4584 * requested blocks (not just some of them) have been allocated.
4585 */
4586static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
4587 bool dio)
4588{
4589 struct inode *inode = file_inode(iocb->ki_filp);
4590 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4591 const loff_t pos = iocb->ki_pos;
4592 const size_t count = iov_iter_count(iter);
4593 struct f2fs_map_blocks map = {};
4594 int flag;
4595 int ret;
4596
4597 /* If it will be an out-of-place direct write, don't bother. */
4598 if (dio && f2fs_lfs_mode(sbi))
4599 return 0;
4600 /*
4601 * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
4602 * buffered IO, if DIO meets any holes.
4603 */
4604 if (dio && i_size_read(inode) &&
4605 (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode))))
4606 return 0;
4607
4608 /* No-wait I/O can't allocate blocks. */
4609 if (iocb->ki_flags & IOCB_NOWAIT)
4610 return 0;
4611
4612 /* If it will be a short write, don't bother. */
4613 if (fault_in_iov_iter_readable(iter, count))
4614 return 0;
4615
4616 if (f2fs_has_inline_data(inode)) {
4617 /* If the data will fit inline, don't bother. */
4618 if (pos + count <= MAX_INLINE_DATA(inode))
4619 return 0;
4620 ret = f2fs_convert_inline_inode(inode);
4621 if (ret)
4622 return ret;
4623 }
4624
4625 /* Do not preallocate blocks that will be written partially in 4KB. */
4626 map.m_lblk = F2FS_BLK_ALIGN(pos);
4627 map.m_len = F2FS_BYTES_TO_BLK(pos + count);
4628 if (map.m_len > map.m_lblk)
4629 map.m_len -= map.m_lblk;
4630 else
4631 return 0;
4632
4633 map.m_may_create = true;
4634 if (dio) {
4635 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4636 flag = F2FS_GET_BLOCK_PRE_DIO;
4637 } else {
4638 map.m_seg_type = NO_CHECK_TYPE;
4639 flag = F2FS_GET_BLOCK_PRE_AIO;
4640 }
4641
4642 ret = f2fs_map_blocks(inode, &map, flag);
4643 /* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */
4644 if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
4645 return ret;
4646 if (ret == 0)
4647 set_inode_flag(inode, FI_PREALLOCATED_ALL);
4648 return map.m_len;
4649}
4650
4651static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
4652 struct iov_iter *from)
4653{
4654 struct file *file = iocb->ki_filp;
4655 struct inode *inode = file_inode(file);
4656 ssize_t ret;
4657
4658 if (iocb->ki_flags & IOCB_NOWAIT)
4659 return -EOPNOTSUPP;
4660
4661 ret = generic_perform_write(iocb, from);
4662
4663 if (ret > 0) {
4664 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4665 APP_BUFFERED_IO, ret);
4666 }
4667 return ret;
4668}
4669
4670static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
4671 unsigned int flags)
4672{
4673 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4674
4675 dec_page_count(sbi, F2FS_DIO_WRITE);
4676 if (error)
4677 return error;
4678 f2fs_update_time(sbi, REQ_TIME);
4679 f2fs_update_iostat(sbi, NULL, APP_DIRECT_IO, size);
4680 return 0;
4681}
4682
4683static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
4684 .end_io = f2fs_dio_write_end_io,
4685};
4686
4687static void f2fs_flush_buffered_write(struct address_space *mapping,
4688 loff_t start_pos, loff_t end_pos)
4689{
4690 int ret;
4691
4692 ret = filemap_write_and_wait_range(mapping, start_pos, end_pos);
4693 if (ret < 0)
4694 return;
4695 invalidate_mapping_pages(mapping,
4696 start_pos >> PAGE_SHIFT,
4697 end_pos >> PAGE_SHIFT);
4698}
4699
4700static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
4701 bool *may_need_sync)
4702{
4703 struct file *file = iocb->ki_filp;
4704 struct inode *inode = file_inode(file);
4705 struct f2fs_inode_info *fi = F2FS_I(inode);
4706 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4707 const bool do_opu = f2fs_lfs_mode(sbi);
4708 const loff_t pos = iocb->ki_pos;
4709 const ssize_t count = iov_iter_count(from);
4710 unsigned int dio_flags;
4711 struct iomap_dio *dio;
4712 ssize_t ret;
4713
4714 trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
4715
4716 if (iocb->ki_flags & IOCB_NOWAIT) {
4717 /* f2fs_convert_inline_inode() and block allocation can block */
4718 if (f2fs_has_inline_data(inode) ||
4719 !f2fs_overwrite_io(inode, pos, count)) {
4720 ret = -EAGAIN;
4721 goto out;
4722 }
4723
4724 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
4725 ret = -EAGAIN;
4726 goto out;
4727 }
4728 if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4729 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4730 ret = -EAGAIN;
4731 goto out;
4732 }
4733 } else {
4734 ret = f2fs_convert_inline_inode(inode);
4735 if (ret)
4736 goto out;
4737
4738 f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
4739 if (do_opu)
4740 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4741 }
4742
4743 /*
4744 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4745 * the higher-level function iomap_dio_rw() in order to ensure that the
4746 * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
4747 */
4748 inc_page_count(sbi, F2FS_DIO_WRITE);
4749 dio_flags = 0;
4750 if (pos + count > inode->i_size)
4751 dio_flags |= IOMAP_DIO_FORCE_WAIT;
4752 dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
4753 &f2fs_iomap_dio_write_ops, dio_flags, NULL, 0);
4754 if (IS_ERR_OR_NULL(dio)) {
4755 ret = PTR_ERR_OR_ZERO(dio);
4756 if (ret == -ENOTBLK)
4757 ret = 0;
4758 if (ret != -EIOCBQUEUED)
4759 dec_page_count(sbi, F2FS_DIO_WRITE);
4760 } else {
4761 ret = iomap_dio_complete(dio);
4762 }
4763
4764 if (do_opu)
4765 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4766 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4767
4768 if (ret < 0)
4769 goto out;
4770 if (pos + ret > inode->i_size)
4771 f2fs_i_size_write(inode, pos + ret);
4772 if (!do_opu)
4773 set_inode_flag(inode, FI_UPDATE_WRITE);
4774
4775 if (iov_iter_count(from)) {
4776 ssize_t ret2;
4777 loff_t bufio_start_pos = iocb->ki_pos;
4778
4779 /*
4780 * The direct write was partial, so we need to fall back to a
4781 * buffered write for the remainder.
4782 */
4783
4784 ret2 = f2fs_buffered_write_iter(iocb, from);
4785 if (iov_iter_count(from))
4786 f2fs_write_failed(inode, iocb->ki_pos);
4787 if (ret2 < 0)
4788 goto out;
4789
4790 /*
4791 * Ensure that the pagecache pages are written to disk and
4792 * invalidated to preserve the expected O_DIRECT semantics.
4793 */
4794 if (ret2 > 0) {
4795 loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
4796
4797 ret += ret2;
4798
4799 f2fs_flush_buffered_write(file->f_mapping,
4800 bufio_start_pos,
4801 bufio_end_pos);
4802 }
4803 } else {
4804 /* iomap_dio_rw() already handled the generic_write_sync(). */
4805 *may_need_sync = false;
4806 }
4807out:
4808 trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
4809 return ret;
4810}
4811
4812static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4813{
4814 struct inode *inode = file_inode(iocb->ki_filp);
4815 const loff_t orig_pos = iocb->ki_pos;
4816 const size_t orig_count = iov_iter_count(from);
4817 loff_t target_size;
4818 bool dio;
4819 bool may_need_sync = true;
4820 int preallocated;
4821 ssize_t ret;
4822
4823 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4824 ret = -EIO;
4825 goto out;
4826 }
4827
4828 if (!f2fs_is_compress_backend_ready(inode)) {
4829 ret = -EOPNOTSUPP;
4830 goto out;
4831 }
4832
4833 if (iocb->ki_flags & IOCB_NOWAIT) {
4834 if (!inode_trylock(inode)) {
4835 ret = -EAGAIN;
4836 goto out;
4837 }
4838 } else {
4839 inode_lock(inode);
4840 }
4841
4842 ret = f2fs_write_checks(iocb, from);
4843 if (ret <= 0)
4844 goto out_unlock;
4845
4846 /* Determine whether we will do a direct write or a buffered write. */
4847 dio = f2fs_should_use_dio(inode, iocb, from);
4848
4849 /* Possibly preallocate the blocks for the write. */
4850 target_size = iocb->ki_pos + iov_iter_count(from);
4851 preallocated = f2fs_preallocate_blocks(iocb, from, dio);
4852 if (preallocated < 0) {
4853 ret = preallocated;
4854 } else {
4855 if (trace_f2fs_datawrite_start_enabled())
4856 f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
4857 orig_count, WRITE);
4858
4859 /* Do the actual write. */
4860 ret = dio ?
4861 f2fs_dio_write_iter(iocb, from, &may_need_sync) :
4862 f2fs_buffered_write_iter(iocb, from);
4863
4864 if (trace_f2fs_datawrite_end_enabled())
4865 trace_f2fs_datawrite_end(inode, orig_pos, ret);
4866 }
4867
4868 /* Don't leave any preallocated blocks around past i_size. */
4869 if (preallocated && i_size_read(inode) < target_size) {
4870 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4871 filemap_invalidate_lock(inode->i_mapping);
4872 if (!f2fs_truncate(inode))
4873 file_dont_truncate(inode);
4874 filemap_invalidate_unlock(inode->i_mapping);
4875 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4876 } else {
4877 file_dont_truncate(inode);
4878 }
4879
4880 clear_inode_flag(inode, FI_PREALLOCATED_ALL);
4881out_unlock:
4882 inode_unlock(inode);
4883out:
4884 trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
4885
4886 if (ret > 0 && may_need_sync)
4887 ret = generic_write_sync(iocb, ret);
4888
4889 /* If buffered IO was forced, flush and drop the data from
4890 * the page cache to preserve O_DIRECT semantics
4891 */
4892 if (ret > 0 && !dio && (iocb->ki_flags & IOCB_DIRECT))
4893 f2fs_flush_buffered_write(iocb->ki_filp->f_mapping,
4894 orig_pos,
4895 orig_pos + ret - 1);
4896
4897 return ret;
4898}
4899
4900static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
4901 int advice)
4902{
4903 struct address_space *mapping;
4904 struct backing_dev_info *bdi;
4905 struct inode *inode = file_inode(filp);
4906 int err;
4907
4908 if (advice == POSIX_FADV_SEQUENTIAL) {
4909 if (S_ISFIFO(inode->i_mode))
4910 return -ESPIPE;
4911
4912 mapping = filp->f_mapping;
4913 if (!mapping || len < 0)
4914 return -EINVAL;
4915
4916 bdi = inode_to_bdi(mapping->host);
4917 filp->f_ra.ra_pages = bdi->ra_pages *
4918 F2FS_I_SB(inode)->seq_file_ra_mul;
4919 spin_lock(&filp->f_lock);
4920 filp->f_mode &= ~FMODE_RANDOM;
4921 spin_unlock(&filp->f_lock);
4922 return 0;
4923 } else if (advice == POSIX_FADV_WILLNEED && offset == 0) {
4924 /* Load extent cache at the first readahead. */
4925 f2fs_precache_extents(inode);
4926 }
4927
4928 err = generic_fadvise(filp, offset, len, advice);
4929 if (!err && advice == POSIX_FADV_DONTNEED &&
4930 test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
4931 f2fs_compressed_file(inode))
4932 f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
4933
4934 return err;
4935}
4936
4937#ifdef CONFIG_COMPAT
4938struct compat_f2fs_gc_range {
4939 u32 sync;
4940 compat_u64 start;
4941 compat_u64 len;
4942};
4943#define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4944 struct compat_f2fs_gc_range)
4945
4946static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4947{
4948 struct compat_f2fs_gc_range __user *urange;
4949 struct f2fs_gc_range range;
4950 int err;
4951
4952 urange = compat_ptr(arg);
4953 err = get_user(range.sync, &urange->sync);
4954 err |= get_user(range.start, &urange->start);
4955 err |= get_user(range.len, &urange->len);
4956 if (err)
4957 return -EFAULT;
4958
4959 return __f2fs_ioc_gc_range(file, &range);
4960}
4961
4962struct compat_f2fs_move_range {
4963 u32 dst_fd;
4964 compat_u64 pos_in;
4965 compat_u64 pos_out;
4966 compat_u64 len;
4967};
4968#define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4969 struct compat_f2fs_move_range)
4970
4971static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4972{
4973 struct compat_f2fs_move_range __user *urange;
4974 struct f2fs_move_range range;
4975 int err;
4976
4977 urange = compat_ptr(arg);
4978 err = get_user(range.dst_fd, &urange->dst_fd);
4979 err |= get_user(range.pos_in, &urange->pos_in);
4980 err |= get_user(range.pos_out, &urange->pos_out);
4981 err |= get_user(range.len, &urange->len);
4982 if (err)
4983 return -EFAULT;
4984
4985 return __f2fs_ioc_move_range(file, &range);
4986}
4987
4988long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4989{
4990 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4991 return -EIO;
4992 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4993 return -ENOSPC;
4994
4995 switch (cmd) {
4996 case FS_IOC32_GETVERSION:
4997 cmd = FS_IOC_GETVERSION;
4998 break;
4999 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
5000 return f2fs_compat_ioc_gc_range(file, arg);
5001 case F2FS_IOC32_MOVE_RANGE:
5002 return f2fs_compat_ioc_move_range(file, arg);
5003 case F2FS_IOC_START_ATOMIC_WRITE:
5004 case F2FS_IOC_START_ATOMIC_REPLACE:
5005 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
5006 case F2FS_IOC_START_VOLATILE_WRITE:
5007 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
5008 case F2FS_IOC_ABORT_ATOMIC_WRITE:
5009 case F2FS_IOC_SHUTDOWN:
5010 case FITRIM:
5011 case FS_IOC_SET_ENCRYPTION_POLICY:
5012 case FS_IOC_GET_ENCRYPTION_PWSALT:
5013 case FS_IOC_GET_ENCRYPTION_POLICY:
5014 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
5015 case FS_IOC_ADD_ENCRYPTION_KEY:
5016 case FS_IOC_REMOVE_ENCRYPTION_KEY:
5017 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
5018 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
5019 case FS_IOC_GET_ENCRYPTION_NONCE:
5020 case F2FS_IOC_GARBAGE_COLLECT:
5021 case F2FS_IOC_WRITE_CHECKPOINT:
5022 case F2FS_IOC_DEFRAGMENT:
5023 case F2FS_IOC_FLUSH_DEVICE:
5024 case F2FS_IOC_GET_FEATURES:
5025 case F2FS_IOC_GET_PIN_FILE:
5026 case F2FS_IOC_SET_PIN_FILE:
5027 case F2FS_IOC_PRECACHE_EXTENTS:
5028 case F2FS_IOC_RESIZE_FS:
5029 case FS_IOC_ENABLE_VERITY:
5030 case FS_IOC_MEASURE_VERITY:
5031 case FS_IOC_READ_VERITY_METADATA:
5032 case FS_IOC_GETFSLABEL:
5033 case FS_IOC_SETFSLABEL:
5034 case F2FS_IOC_GET_COMPRESS_BLOCKS:
5035 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
5036 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
5037 case F2FS_IOC_SEC_TRIM_FILE:
5038 case F2FS_IOC_GET_COMPRESS_OPTION:
5039 case F2FS_IOC_SET_COMPRESS_OPTION:
5040 case F2FS_IOC_DECOMPRESS_FILE:
5041 case F2FS_IOC_COMPRESS_FILE:
5042 break;
5043 default:
5044 return -ENOIOCTLCMD;
5045 }
5046 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5047}
5048#endif
5049
5050const struct file_operations f2fs_file_operations = {
5051 .llseek = f2fs_llseek,
5052 .read_iter = f2fs_file_read_iter,
5053 .write_iter = f2fs_file_write_iter,
5054 .iopoll = iocb_bio_iopoll,
5055 .open = f2fs_file_open,
5056 .release = f2fs_release_file,
5057 .mmap = f2fs_file_mmap,
5058 .flush = f2fs_file_flush,
5059 .fsync = f2fs_sync_file,
5060 .fallocate = f2fs_fallocate,
5061 .unlocked_ioctl = f2fs_ioctl,
5062#ifdef CONFIG_COMPAT
5063 .compat_ioctl = f2fs_compat_ioctl,
5064#endif
5065 .splice_read = f2fs_file_splice_read,
5066 .splice_write = iter_file_splice_write,
5067 .fadvise = f2fs_file_fadvise,
5068};