Loading...
Note: File does not exist in v3.1.
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};