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