Loading...
Note: File does not exist in v3.1.
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * fs/f2fs/file.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/stat.h>
11#include <linux/buffer_head.h>
12#include <linux/writeback.h>
13#include <linux/blkdev.h>
14#include <linux/falloc.h>
15#include <linux/types.h>
16#include <linux/compat.h>
17#include <linux/uaccess.h>
18#include <linux/mount.h>
19#include <linux/pagevec.h>
20#include <linux/uio.h>
21#include <linux/uuid.h>
22#include <linux/file.h>
23#include <linux/nls.h>
24#include <linux/sched/signal.h>
25
26#include "f2fs.h"
27#include "node.h"
28#include "segment.h"
29#include "xattr.h"
30#include "acl.h"
31#include "gc.h"
32#include "trace.h"
33#include <trace/events/f2fs.h>
34
35static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
36{
37 struct inode *inode = file_inode(vmf->vma->vm_file);
38 vm_fault_t ret;
39
40 down_read(&F2FS_I(inode)->i_mmap_sem);
41 ret = filemap_fault(vmf);
42 up_read(&F2FS_I(inode)->i_mmap_sem);
43
44 if (!ret)
45 f2fs_update_iostat(F2FS_I_SB(inode), APP_MAPPED_READ_IO,
46 F2FS_BLKSIZE);
47
48 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret);
49
50 return ret;
51}
52
53static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
54{
55 struct page *page = vmf->page;
56 struct inode *inode = file_inode(vmf->vma->vm_file);
57 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
58 struct dnode_of_data dn;
59 bool need_alloc = true;
60 int err = 0;
61
62 if (unlikely(f2fs_cp_error(sbi))) {
63 err = -EIO;
64 goto err;
65 }
66
67 if (!f2fs_is_checkpoint_ready(sbi)) {
68 err = -ENOSPC;
69 goto err;
70 }
71
72#ifdef CONFIG_F2FS_FS_COMPRESSION
73 if (f2fs_compressed_file(inode)) {
74 int ret = f2fs_is_compressed_cluster(inode, page->index);
75
76 if (ret < 0) {
77 err = ret;
78 goto err;
79 } else if (ret) {
80 if (ret < F2FS_I(inode)->i_cluster_size) {
81 err = -EAGAIN;
82 goto err;
83 }
84 need_alloc = false;
85 }
86 }
87#endif
88 /* should do out of any locked page */
89 if (need_alloc)
90 f2fs_balance_fs(sbi, true);
91
92 sb_start_pagefault(inode->i_sb);
93
94 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
95
96 file_update_time(vmf->vma->vm_file);
97 down_read(&F2FS_I(inode)->i_mmap_sem);
98 lock_page(page);
99 if (unlikely(page->mapping != inode->i_mapping ||
100 page_offset(page) > i_size_read(inode) ||
101 !PageUptodate(page))) {
102 unlock_page(page);
103 err = -EFAULT;
104 goto out_sem;
105 }
106
107 if (need_alloc) {
108 /* block allocation */
109 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
110 set_new_dnode(&dn, inode, NULL, NULL, 0);
111 err = f2fs_get_block(&dn, page->index);
112 f2fs_put_dnode(&dn);
113 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
114 }
115
116#ifdef CONFIG_F2FS_FS_COMPRESSION
117 if (!need_alloc) {
118 set_new_dnode(&dn, inode, NULL, NULL, 0);
119 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
120 f2fs_put_dnode(&dn);
121 }
122#endif
123 if (err) {
124 unlock_page(page);
125 goto out_sem;
126 }
127
128 f2fs_wait_on_page_writeback(page, DATA, false, true);
129
130 /* wait for GCed page writeback via META_MAPPING */
131 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
132
133 /*
134 * check to see if the page is mapped already (no holes)
135 */
136 if (PageMappedToDisk(page))
137 goto out_sem;
138
139 /* page is wholly or partially inside EOF */
140 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
141 i_size_read(inode)) {
142 loff_t offset;
143
144 offset = i_size_read(inode) & ~PAGE_MASK;
145 zero_user_segment(page, offset, PAGE_SIZE);
146 }
147 set_page_dirty(page);
148 if (!PageUptodate(page))
149 SetPageUptodate(page);
150
151 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
152 f2fs_update_time(sbi, REQ_TIME);
153
154 trace_f2fs_vm_page_mkwrite(page, DATA);
155out_sem:
156 up_read(&F2FS_I(inode)->i_mmap_sem);
157
158 sb_end_pagefault(inode->i_sb);
159err:
160 return block_page_mkwrite_return(err);
161}
162
163static const struct vm_operations_struct f2fs_file_vm_ops = {
164 .fault = f2fs_filemap_fault,
165 .map_pages = filemap_map_pages,
166 .page_mkwrite = f2fs_vm_page_mkwrite,
167};
168
169static int get_parent_ino(struct inode *inode, nid_t *pino)
170{
171 struct dentry *dentry;
172
173 /*
174 * Make sure to get the non-deleted alias. The alias associated with
175 * the open file descriptor being fsync()'ed may be deleted already.
176 */
177 dentry = d_find_alias(inode);
178 if (!dentry)
179 return 0;
180
181 *pino = parent_ino(dentry);
182 dput(dentry);
183 return 1;
184}
185
186static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
187{
188 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
189 enum cp_reason_type cp_reason = CP_NO_NEEDED;
190
191 if (!S_ISREG(inode->i_mode))
192 cp_reason = CP_NON_REGULAR;
193 else if (f2fs_compressed_file(inode))
194 cp_reason = CP_COMPRESSED;
195 else if (inode->i_nlink != 1)
196 cp_reason = CP_HARDLINK;
197 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
198 cp_reason = CP_SB_NEED_CP;
199 else if (file_wrong_pino(inode))
200 cp_reason = CP_WRONG_PINO;
201 else if (!f2fs_space_for_roll_forward(sbi))
202 cp_reason = CP_NO_SPC_ROLL;
203 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
204 cp_reason = CP_NODE_NEED_CP;
205 else if (test_opt(sbi, FASTBOOT))
206 cp_reason = CP_FASTBOOT_MODE;
207 else if (F2FS_OPTION(sbi).active_logs == 2)
208 cp_reason = CP_SPEC_LOG_NUM;
209 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
210 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
211 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
212 TRANS_DIR_INO))
213 cp_reason = CP_RECOVER_DIR;
214
215 return cp_reason;
216}
217
218static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
219{
220 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
221 bool ret = false;
222 /* But we need to avoid that there are some inode updates */
223 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
224 ret = true;
225 f2fs_put_page(i, 0);
226 return ret;
227}
228
229static void try_to_fix_pino(struct inode *inode)
230{
231 struct f2fs_inode_info *fi = F2FS_I(inode);
232 nid_t pino;
233
234 down_write(&fi->i_sem);
235 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
236 get_parent_ino(inode, &pino)) {
237 f2fs_i_pino_write(inode, pino);
238 file_got_pino(inode);
239 }
240 up_write(&fi->i_sem);
241}
242
243static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
244 int datasync, bool atomic)
245{
246 struct inode *inode = file->f_mapping->host;
247 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
248 nid_t ino = inode->i_ino;
249 int ret = 0;
250 enum cp_reason_type cp_reason = 0;
251 struct writeback_control wbc = {
252 .sync_mode = WB_SYNC_ALL,
253 .nr_to_write = LONG_MAX,
254 .for_reclaim = 0,
255 };
256 unsigned int seq_id = 0;
257
258 if (unlikely(f2fs_readonly(inode->i_sb) ||
259 is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
260 return 0;
261
262 trace_f2fs_sync_file_enter(inode);
263
264 if (S_ISDIR(inode->i_mode))
265 goto go_write;
266
267 /* if fdatasync is triggered, let's do in-place-update */
268 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
269 set_inode_flag(inode, FI_NEED_IPU);
270 ret = file_write_and_wait_range(file, start, end);
271 clear_inode_flag(inode, FI_NEED_IPU);
272
273 if (ret) {
274 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
275 return ret;
276 }
277
278 /* if the inode is dirty, let's recover all the time */
279 if (!f2fs_skip_inode_update(inode, datasync)) {
280 f2fs_write_inode(inode, NULL);
281 goto go_write;
282 }
283
284 /*
285 * if there is no written data, don't waste time to write recovery info.
286 */
287 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
288 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
289
290 /* it may call write_inode just prior to fsync */
291 if (need_inode_page_update(sbi, ino))
292 goto go_write;
293
294 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
295 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
296 goto flush_out;
297 goto out;
298 }
299go_write:
300 /*
301 * Both of fdatasync() and fsync() are able to be recovered from
302 * sudden-power-off.
303 */
304 down_read(&F2FS_I(inode)->i_sem);
305 cp_reason = need_do_checkpoint(inode);
306 up_read(&F2FS_I(inode)->i_sem);
307
308 if (cp_reason) {
309 /* all the dirty node pages should be flushed for POR */
310 ret = f2fs_sync_fs(inode->i_sb, 1);
311
312 /*
313 * We've secured consistency through sync_fs. Following pino
314 * will be used only for fsynced inodes after checkpoint.
315 */
316 try_to_fix_pino(inode);
317 clear_inode_flag(inode, FI_APPEND_WRITE);
318 clear_inode_flag(inode, FI_UPDATE_WRITE);
319 goto out;
320 }
321sync_nodes:
322 atomic_inc(&sbi->wb_sync_req[NODE]);
323 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
324 atomic_dec(&sbi->wb_sync_req[NODE]);
325 if (ret)
326 goto out;
327
328 /* if cp_error was enabled, we should avoid infinite loop */
329 if (unlikely(f2fs_cp_error(sbi))) {
330 ret = -EIO;
331 goto out;
332 }
333
334 if (f2fs_need_inode_block_update(sbi, ino)) {
335 f2fs_mark_inode_dirty_sync(inode, true);
336 f2fs_write_inode(inode, NULL);
337 goto sync_nodes;
338 }
339
340 /*
341 * If it's atomic_write, it's just fine to keep write ordering. So
342 * here we don't need to wait for node write completion, since we use
343 * node chain which serializes node blocks. If one of node writes are
344 * reordered, we can see simply broken chain, resulting in stopping
345 * roll-forward recovery. It means we'll recover all or none node blocks
346 * given fsync mark.
347 */
348 if (!atomic) {
349 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
350 if (ret)
351 goto out;
352 }
353
354 /* once recovery info is written, don't need to tack this */
355 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
356 clear_inode_flag(inode, FI_APPEND_WRITE);
357flush_out:
358 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
359 ret = f2fs_issue_flush(sbi, inode->i_ino);
360 if (!ret) {
361 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
362 clear_inode_flag(inode, FI_UPDATE_WRITE);
363 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
364 }
365 f2fs_update_time(sbi, REQ_TIME);
366out:
367 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
368 f2fs_trace_ios(NULL, 1);
369 return ret;
370}
371
372int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
373{
374 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
375 return -EIO;
376 return f2fs_do_sync_file(file, start, end, datasync, false);
377}
378
379static pgoff_t __get_first_dirty_index(struct address_space *mapping,
380 pgoff_t pgofs, int whence)
381{
382 struct page *page;
383 int nr_pages;
384
385 if (whence != SEEK_DATA)
386 return 0;
387
388 /* find first dirty page index */
389 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
390 1, &page);
391 if (!nr_pages)
392 return ULONG_MAX;
393 pgofs = page->index;
394 put_page(page);
395 return pgofs;
396}
397
398static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
399 pgoff_t dirty, pgoff_t pgofs, int whence)
400{
401 switch (whence) {
402 case SEEK_DATA:
403 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
404 __is_valid_data_blkaddr(blkaddr))
405 return true;
406 break;
407 case SEEK_HOLE:
408 if (blkaddr == NULL_ADDR)
409 return true;
410 break;
411 }
412 return false;
413}
414
415static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
416{
417 struct inode *inode = file->f_mapping->host;
418 loff_t maxbytes = inode->i_sb->s_maxbytes;
419 struct dnode_of_data dn;
420 pgoff_t pgofs, end_offset, dirty;
421 loff_t data_ofs = offset;
422 loff_t isize;
423 int err = 0;
424
425 inode_lock(inode);
426
427 isize = i_size_read(inode);
428 if (offset >= isize)
429 goto fail;
430
431 /* handle inline data case */
432 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
433 if (whence == SEEK_HOLE)
434 data_ofs = isize;
435 goto found;
436 }
437
438 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
439
440 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
441
442 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
443 set_new_dnode(&dn, inode, NULL, NULL, 0);
444 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
445 if (err && err != -ENOENT) {
446 goto fail;
447 } else if (err == -ENOENT) {
448 /* direct node does not exists */
449 if (whence == SEEK_DATA) {
450 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
451 continue;
452 } else {
453 goto found;
454 }
455 }
456
457 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
458
459 /* find data/hole in dnode block */
460 for (; dn.ofs_in_node < end_offset;
461 dn.ofs_in_node++, pgofs++,
462 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
463 block_t blkaddr;
464
465 blkaddr = f2fs_data_blkaddr(&dn);
466
467 if (__is_valid_data_blkaddr(blkaddr) &&
468 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
469 blkaddr, DATA_GENERIC_ENHANCE)) {
470 f2fs_put_dnode(&dn);
471 goto fail;
472 }
473
474 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
475 pgofs, whence)) {
476 f2fs_put_dnode(&dn);
477 goto found;
478 }
479 }
480 f2fs_put_dnode(&dn);
481 }
482
483 if (whence == SEEK_DATA)
484 goto fail;
485found:
486 if (whence == SEEK_HOLE && data_ofs > isize)
487 data_ofs = isize;
488 inode_unlock(inode);
489 return vfs_setpos(file, data_ofs, maxbytes);
490fail:
491 inode_unlock(inode);
492 return -ENXIO;
493}
494
495static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
496{
497 struct inode *inode = file->f_mapping->host;
498 loff_t maxbytes = inode->i_sb->s_maxbytes;
499
500 switch (whence) {
501 case SEEK_SET:
502 case SEEK_CUR:
503 case SEEK_END:
504 return generic_file_llseek_size(file, offset, whence,
505 maxbytes, i_size_read(inode));
506 case SEEK_DATA:
507 case SEEK_HOLE:
508 if (offset < 0)
509 return -ENXIO;
510 return f2fs_seek_block(file, offset, whence);
511 }
512
513 return -EINVAL;
514}
515
516static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
517{
518 struct inode *inode = file_inode(file);
519 int err;
520
521 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
522 return -EIO;
523
524 if (!f2fs_is_compress_backend_ready(inode))
525 return -EOPNOTSUPP;
526
527 /* we don't need to use inline_data strictly */
528 err = f2fs_convert_inline_inode(inode);
529 if (err)
530 return err;
531
532 file_accessed(file);
533 vma->vm_ops = &f2fs_file_vm_ops;
534 set_inode_flag(inode, FI_MMAP_FILE);
535 return 0;
536}
537
538static int f2fs_file_open(struct inode *inode, struct file *filp)
539{
540 int err = fscrypt_file_open(inode, filp);
541
542 if (err)
543 return err;
544
545 if (!f2fs_is_compress_backend_ready(inode))
546 return -EOPNOTSUPP;
547
548 err = fsverity_file_open(inode, filp);
549 if (err)
550 return err;
551
552 filp->f_mode |= FMODE_NOWAIT;
553
554 return dquot_file_open(inode, filp);
555}
556
557void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
558{
559 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
560 struct f2fs_node *raw_node;
561 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
562 __le32 *addr;
563 int base = 0;
564 bool compressed_cluster = false;
565 int cluster_index = 0, valid_blocks = 0;
566 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
567 bool released = !F2FS_I(dn->inode)->i_compr_blocks;
568
569 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
570 base = get_extra_isize(dn->inode);
571
572 raw_node = F2FS_NODE(dn->node_page);
573 addr = blkaddr_in_node(raw_node) + base + ofs;
574
575 /* Assumption: truncateion starts with cluster */
576 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
577 block_t blkaddr = le32_to_cpu(*addr);
578
579 if (f2fs_compressed_file(dn->inode) &&
580 !(cluster_index & (cluster_size - 1))) {
581 if (compressed_cluster)
582 f2fs_i_compr_blocks_update(dn->inode,
583 valid_blocks, false);
584 compressed_cluster = (blkaddr == COMPRESS_ADDR);
585 valid_blocks = 0;
586 }
587
588 if (blkaddr == NULL_ADDR)
589 continue;
590
591 dn->data_blkaddr = NULL_ADDR;
592 f2fs_set_data_blkaddr(dn);
593
594 if (__is_valid_data_blkaddr(blkaddr)) {
595 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
596 DATA_GENERIC_ENHANCE))
597 continue;
598 if (compressed_cluster)
599 valid_blocks++;
600 }
601
602 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
603 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
604
605 f2fs_invalidate_blocks(sbi, blkaddr);
606
607 if (!released || blkaddr != COMPRESS_ADDR)
608 nr_free++;
609 }
610
611 if (compressed_cluster)
612 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
613
614 if (nr_free) {
615 pgoff_t fofs;
616 /*
617 * once we invalidate valid blkaddr in range [ofs, ofs + count],
618 * we will invalidate all blkaddr in the whole range.
619 */
620 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
621 dn->inode) + ofs;
622 f2fs_update_extent_cache_range(dn, fofs, 0, len);
623 dec_valid_block_count(sbi, dn->inode, nr_free);
624 }
625 dn->ofs_in_node = ofs;
626
627 f2fs_update_time(sbi, REQ_TIME);
628 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
629 dn->ofs_in_node, nr_free);
630}
631
632void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
633{
634 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
635}
636
637static int truncate_partial_data_page(struct inode *inode, u64 from,
638 bool cache_only)
639{
640 loff_t offset = from & (PAGE_SIZE - 1);
641 pgoff_t index = from >> PAGE_SHIFT;
642 struct address_space *mapping = inode->i_mapping;
643 struct page *page;
644
645 if (!offset && !cache_only)
646 return 0;
647
648 if (cache_only) {
649 page = find_lock_page(mapping, index);
650 if (page && PageUptodate(page))
651 goto truncate_out;
652 f2fs_put_page(page, 1);
653 return 0;
654 }
655
656 page = f2fs_get_lock_data_page(inode, index, true);
657 if (IS_ERR(page))
658 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
659truncate_out:
660 f2fs_wait_on_page_writeback(page, DATA, true, true);
661 zero_user(page, offset, PAGE_SIZE - offset);
662
663 /* An encrypted inode should have a key and truncate the last page. */
664 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
665 if (!cache_only)
666 set_page_dirty(page);
667 f2fs_put_page(page, 1);
668 return 0;
669}
670
671int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
672{
673 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
674 struct dnode_of_data dn;
675 pgoff_t free_from;
676 int count = 0, err = 0;
677 struct page *ipage;
678 bool truncate_page = false;
679
680 trace_f2fs_truncate_blocks_enter(inode, from);
681
682 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
683
684 if (free_from >= sbi->max_file_blocks)
685 goto free_partial;
686
687 if (lock)
688 f2fs_lock_op(sbi);
689
690 ipage = f2fs_get_node_page(sbi, inode->i_ino);
691 if (IS_ERR(ipage)) {
692 err = PTR_ERR(ipage);
693 goto out;
694 }
695
696 if (f2fs_has_inline_data(inode)) {
697 f2fs_truncate_inline_inode(inode, ipage, from);
698 f2fs_put_page(ipage, 1);
699 truncate_page = true;
700 goto out;
701 }
702
703 set_new_dnode(&dn, inode, ipage, NULL, 0);
704 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
705 if (err) {
706 if (err == -ENOENT)
707 goto free_next;
708 goto out;
709 }
710
711 count = ADDRS_PER_PAGE(dn.node_page, inode);
712
713 count -= dn.ofs_in_node;
714 f2fs_bug_on(sbi, count < 0);
715
716 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
717 f2fs_truncate_data_blocks_range(&dn, count);
718 free_from += count;
719 }
720
721 f2fs_put_dnode(&dn);
722free_next:
723 err = f2fs_truncate_inode_blocks(inode, free_from);
724out:
725 if (lock)
726 f2fs_unlock_op(sbi);
727free_partial:
728 /* lastly zero out the first data page */
729 if (!err)
730 err = truncate_partial_data_page(inode, from, truncate_page);
731
732 trace_f2fs_truncate_blocks_exit(inode, err);
733 return err;
734}
735
736int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
737{
738 u64 free_from = from;
739 int err;
740
741#ifdef CONFIG_F2FS_FS_COMPRESSION
742 /*
743 * for compressed file, only support cluster size
744 * aligned truncation.
745 */
746 if (f2fs_compressed_file(inode))
747 free_from = round_up(from,
748 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
749#endif
750
751 err = f2fs_do_truncate_blocks(inode, free_from, lock);
752 if (err)
753 return err;
754
755#ifdef CONFIG_F2FS_FS_COMPRESSION
756 if (from != free_from)
757 err = f2fs_truncate_partial_cluster(inode, from, lock);
758#endif
759
760 return err;
761}
762
763int f2fs_truncate(struct inode *inode)
764{
765 int err;
766
767 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
768 return -EIO;
769
770 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
771 S_ISLNK(inode->i_mode)))
772 return 0;
773
774 trace_f2fs_truncate(inode);
775
776 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
777 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE);
778 return -EIO;
779 }
780
781 /* we should check inline_data size */
782 if (!f2fs_may_inline_data(inode)) {
783 err = f2fs_convert_inline_inode(inode);
784 if (err)
785 return err;
786 }
787
788 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
789 if (err)
790 return err;
791
792 inode->i_mtime = inode->i_ctime = current_time(inode);
793 f2fs_mark_inode_dirty_sync(inode, false);
794 return 0;
795}
796
797int f2fs_getattr(const struct path *path, struct kstat *stat,
798 u32 request_mask, unsigned int query_flags)
799{
800 struct inode *inode = d_inode(path->dentry);
801 struct f2fs_inode_info *fi = F2FS_I(inode);
802 struct f2fs_inode *ri;
803 unsigned int flags;
804
805 if (f2fs_has_extra_attr(inode) &&
806 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
807 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
808 stat->result_mask |= STATX_BTIME;
809 stat->btime.tv_sec = fi->i_crtime.tv_sec;
810 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
811 }
812
813 flags = fi->i_flags;
814 if (flags & F2FS_COMPR_FL)
815 stat->attributes |= STATX_ATTR_COMPRESSED;
816 if (flags & F2FS_APPEND_FL)
817 stat->attributes |= STATX_ATTR_APPEND;
818 if (IS_ENCRYPTED(inode))
819 stat->attributes |= STATX_ATTR_ENCRYPTED;
820 if (flags & F2FS_IMMUTABLE_FL)
821 stat->attributes |= STATX_ATTR_IMMUTABLE;
822 if (flags & F2FS_NODUMP_FL)
823 stat->attributes |= STATX_ATTR_NODUMP;
824 if (IS_VERITY(inode))
825 stat->attributes |= STATX_ATTR_VERITY;
826
827 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
828 STATX_ATTR_APPEND |
829 STATX_ATTR_ENCRYPTED |
830 STATX_ATTR_IMMUTABLE |
831 STATX_ATTR_NODUMP |
832 STATX_ATTR_VERITY);
833
834 generic_fillattr(inode, stat);
835
836 /* we need to show initial sectors used for inline_data/dentries */
837 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
838 f2fs_has_inline_dentry(inode))
839 stat->blocks += (stat->size + 511) >> 9;
840
841 return 0;
842}
843
844#ifdef CONFIG_F2FS_FS_POSIX_ACL
845static void __setattr_copy(struct inode *inode, const struct iattr *attr)
846{
847 unsigned int ia_valid = attr->ia_valid;
848
849 if (ia_valid & ATTR_UID)
850 inode->i_uid = attr->ia_uid;
851 if (ia_valid & ATTR_GID)
852 inode->i_gid = attr->ia_gid;
853 if (ia_valid & ATTR_ATIME)
854 inode->i_atime = attr->ia_atime;
855 if (ia_valid & ATTR_MTIME)
856 inode->i_mtime = attr->ia_mtime;
857 if (ia_valid & ATTR_CTIME)
858 inode->i_ctime = attr->ia_ctime;
859 if (ia_valid & ATTR_MODE) {
860 umode_t mode = attr->ia_mode;
861
862 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
863 mode &= ~S_ISGID;
864 set_acl_inode(inode, mode);
865 }
866}
867#else
868#define __setattr_copy setattr_copy
869#endif
870
871int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
872{
873 struct inode *inode = d_inode(dentry);
874 int err;
875
876 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
877 return -EIO;
878
879 if ((attr->ia_valid & ATTR_SIZE) &&
880 !f2fs_is_compress_backend_ready(inode))
881 return -EOPNOTSUPP;
882
883 err = setattr_prepare(dentry, attr);
884 if (err)
885 return err;
886
887 err = fscrypt_prepare_setattr(dentry, attr);
888 if (err)
889 return err;
890
891 err = fsverity_prepare_setattr(dentry, attr);
892 if (err)
893 return err;
894
895 if (is_quota_modification(inode, attr)) {
896 err = dquot_initialize(inode);
897 if (err)
898 return err;
899 }
900 if ((attr->ia_valid & ATTR_UID &&
901 !uid_eq(attr->ia_uid, inode->i_uid)) ||
902 (attr->ia_valid & ATTR_GID &&
903 !gid_eq(attr->ia_gid, inode->i_gid))) {
904 f2fs_lock_op(F2FS_I_SB(inode));
905 err = dquot_transfer(inode, attr);
906 if (err) {
907 set_sbi_flag(F2FS_I_SB(inode),
908 SBI_QUOTA_NEED_REPAIR);
909 f2fs_unlock_op(F2FS_I_SB(inode));
910 return err;
911 }
912 /*
913 * update uid/gid under lock_op(), so that dquot and inode can
914 * be updated atomically.
915 */
916 if (attr->ia_valid & ATTR_UID)
917 inode->i_uid = attr->ia_uid;
918 if (attr->ia_valid & ATTR_GID)
919 inode->i_gid = attr->ia_gid;
920 f2fs_mark_inode_dirty_sync(inode, true);
921 f2fs_unlock_op(F2FS_I_SB(inode));
922 }
923
924 if (attr->ia_valid & ATTR_SIZE) {
925 loff_t old_size = i_size_read(inode);
926
927 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
928 /*
929 * should convert inline inode before i_size_write to
930 * keep smaller than inline_data size with inline flag.
931 */
932 err = f2fs_convert_inline_inode(inode);
933 if (err)
934 return err;
935 }
936
937 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
938 down_write(&F2FS_I(inode)->i_mmap_sem);
939
940 truncate_setsize(inode, attr->ia_size);
941
942 if (attr->ia_size <= old_size)
943 err = f2fs_truncate(inode);
944 /*
945 * do not trim all blocks after i_size if target size is
946 * larger than i_size.
947 */
948 up_write(&F2FS_I(inode)->i_mmap_sem);
949 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
950 if (err)
951 return err;
952
953 spin_lock(&F2FS_I(inode)->i_size_lock);
954 inode->i_mtime = inode->i_ctime = current_time(inode);
955 F2FS_I(inode)->last_disk_size = i_size_read(inode);
956 spin_unlock(&F2FS_I(inode)->i_size_lock);
957 }
958
959 __setattr_copy(inode, attr);
960
961 if (attr->ia_valid & ATTR_MODE) {
962 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
963 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
964 inode->i_mode = F2FS_I(inode)->i_acl_mode;
965 clear_inode_flag(inode, FI_ACL_MODE);
966 }
967 }
968
969 /* file size may changed here */
970 f2fs_mark_inode_dirty_sync(inode, true);
971
972 /* inode change will produce dirty node pages flushed by checkpoint */
973 f2fs_balance_fs(F2FS_I_SB(inode), true);
974
975 return err;
976}
977
978const struct inode_operations f2fs_file_inode_operations = {
979 .getattr = f2fs_getattr,
980 .setattr = f2fs_setattr,
981 .get_acl = f2fs_get_acl,
982 .set_acl = f2fs_set_acl,
983 .listxattr = f2fs_listxattr,
984 .fiemap = f2fs_fiemap,
985};
986
987static int fill_zero(struct inode *inode, pgoff_t index,
988 loff_t start, loff_t len)
989{
990 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
991 struct page *page;
992
993 if (!len)
994 return 0;
995
996 f2fs_balance_fs(sbi, true);
997
998 f2fs_lock_op(sbi);
999 page = f2fs_get_new_data_page(inode, NULL, index, false);
1000 f2fs_unlock_op(sbi);
1001
1002 if (IS_ERR(page))
1003 return PTR_ERR(page);
1004
1005 f2fs_wait_on_page_writeback(page, DATA, true, true);
1006 zero_user(page, start, len);
1007 set_page_dirty(page);
1008 f2fs_put_page(page, 1);
1009 return 0;
1010}
1011
1012int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1013{
1014 int err;
1015
1016 while (pg_start < pg_end) {
1017 struct dnode_of_data dn;
1018 pgoff_t end_offset, count;
1019
1020 set_new_dnode(&dn, inode, NULL, NULL, 0);
1021 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1022 if (err) {
1023 if (err == -ENOENT) {
1024 pg_start = f2fs_get_next_page_offset(&dn,
1025 pg_start);
1026 continue;
1027 }
1028 return err;
1029 }
1030
1031 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1032 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1033
1034 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1035
1036 f2fs_truncate_data_blocks_range(&dn, count);
1037 f2fs_put_dnode(&dn);
1038
1039 pg_start += count;
1040 }
1041 return 0;
1042}
1043
1044static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
1045{
1046 pgoff_t pg_start, pg_end;
1047 loff_t off_start, off_end;
1048 int ret;
1049
1050 ret = f2fs_convert_inline_inode(inode);
1051 if (ret)
1052 return ret;
1053
1054 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1055 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1056
1057 off_start = offset & (PAGE_SIZE - 1);
1058 off_end = (offset + len) & (PAGE_SIZE - 1);
1059
1060 if (pg_start == pg_end) {
1061 ret = fill_zero(inode, pg_start, off_start,
1062 off_end - off_start);
1063 if (ret)
1064 return ret;
1065 } else {
1066 if (off_start) {
1067 ret = fill_zero(inode, pg_start++, off_start,
1068 PAGE_SIZE - off_start);
1069 if (ret)
1070 return ret;
1071 }
1072 if (off_end) {
1073 ret = fill_zero(inode, pg_end, 0, off_end);
1074 if (ret)
1075 return ret;
1076 }
1077
1078 if (pg_start < pg_end) {
1079 struct address_space *mapping = inode->i_mapping;
1080 loff_t blk_start, blk_end;
1081 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1082
1083 f2fs_balance_fs(sbi, true);
1084
1085 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1086 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1087
1088 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1089 down_write(&F2FS_I(inode)->i_mmap_sem);
1090
1091 truncate_inode_pages_range(mapping, blk_start,
1092 blk_end - 1);
1093
1094 f2fs_lock_op(sbi);
1095 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1096 f2fs_unlock_op(sbi);
1097
1098 up_write(&F2FS_I(inode)->i_mmap_sem);
1099 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1100 }
1101 }
1102
1103 return ret;
1104}
1105
1106static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1107 int *do_replace, pgoff_t off, pgoff_t len)
1108{
1109 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1110 struct dnode_of_data dn;
1111 int ret, done, i;
1112
1113next_dnode:
1114 set_new_dnode(&dn, inode, NULL, NULL, 0);
1115 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1116 if (ret && ret != -ENOENT) {
1117 return ret;
1118 } else if (ret == -ENOENT) {
1119 if (dn.max_level == 0)
1120 return -ENOENT;
1121 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1122 dn.ofs_in_node, len);
1123 blkaddr += done;
1124 do_replace += done;
1125 goto next;
1126 }
1127
1128 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1129 dn.ofs_in_node, len);
1130 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1131 *blkaddr = f2fs_data_blkaddr(&dn);
1132
1133 if (__is_valid_data_blkaddr(*blkaddr) &&
1134 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1135 DATA_GENERIC_ENHANCE)) {
1136 f2fs_put_dnode(&dn);
1137 return -EFSCORRUPTED;
1138 }
1139
1140 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1141
1142 if (f2fs_lfs_mode(sbi)) {
1143 f2fs_put_dnode(&dn);
1144 return -EOPNOTSUPP;
1145 }
1146
1147 /* do not invalidate this block address */
1148 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1149 *do_replace = 1;
1150 }
1151 }
1152 f2fs_put_dnode(&dn);
1153next:
1154 len -= done;
1155 off += done;
1156 if (len)
1157 goto next_dnode;
1158 return 0;
1159}
1160
1161static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1162 int *do_replace, pgoff_t off, int len)
1163{
1164 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1165 struct dnode_of_data dn;
1166 int ret, i;
1167
1168 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1169 if (*do_replace == 0)
1170 continue;
1171
1172 set_new_dnode(&dn, inode, NULL, NULL, 0);
1173 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1174 if (ret) {
1175 dec_valid_block_count(sbi, inode, 1);
1176 f2fs_invalidate_blocks(sbi, *blkaddr);
1177 } else {
1178 f2fs_update_data_blkaddr(&dn, *blkaddr);
1179 }
1180 f2fs_put_dnode(&dn);
1181 }
1182 return 0;
1183}
1184
1185static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1186 block_t *blkaddr, int *do_replace,
1187 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1188{
1189 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1190 pgoff_t i = 0;
1191 int ret;
1192
1193 while (i < len) {
1194 if (blkaddr[i] == NULL_ADDR && !full) {
1195 i++;
1196 continue;
1197 }
1198
1199 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1200 struct dnode_of_data dn;
1201 struct node_info ni;
1202 size_t new_size;
1203 pgoff_t ilen;
1204
1205 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1206 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1207 if (ret)
1208 return ret;
1209
1210 ret = f2fs_get_node_info(sbi, dn.nid, &ni);
1211 if (ret) {
1212 f2fs_put_dnode(&dn);
1213 return ret;
1214 }
1215
1216 ilen = min((pgoff_t)
1217 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1218 dn.ofs_in_node, len - i);
1219 do {
1220 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1221 f2fs_truncate_data_blocks_range(&dn, 1);
1222
1223 if (do_replace[i]) {
1224 f2fs_i_blocks_write(src_inode,
1225 1, false, false);
1226 f2fs_i_blocks_write(dst_inode,
1227 1, true, false);
1228 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1229 blkaddr[i], ni.version, true, false);
1230
1231 do_replace[i] = 0;
1232 }
1233 dn.ofs_in_node++;
1234 i++;
1235 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1236 if (dst_inode->i_size < new_size)
1237 f2fs_i_size_write(dst_inode, new_size);
1238 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1239
1240 f2fs_put_dnode(&dn);
1241 } else {
1242 struct page *psrc, *pdst;
1243
1244 psrc = f2fs_get_lock_data_page(src_inode,
1245 src + i, true);
1246 if (IS_ERR(psrc))
1247 return PTR_ERR(psrc);
1248 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1249 true);
1250 if (IS_ERR(pdst)) {
1251 f2fs_put_page(psrc, 1);
1252 return PTR_ERR(pdst);
1253 }
1254 f2fs_copy_page(psrc, pdst);
1255 set_page_dirty(pdst);
1256 f2fs_put_page(pdst, 1);
1257 f2fs_put_page(psrc, 1);
1258
1259 ret = f2fs_truncate_hole(src_inode,
1260 src + i, src + i + 1);
1261 if (ret)
1262 return ret;
1263 i++;
1264 }
1265 }
1266 return 0;
1267}
1268
1269static int __exchange_data_block(struct inode *src_inode,
1270 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1271 pgoff_t len, bool full)
1272{
1273 block_t *src_blkaddr;
1274 int *do_replace;
1275 pgoff_t olen;
1276 int ret;
1277
1278 while (len) {
1279 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1280
1281 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1282 array_size(olen, sizeof(block_t)),
1283 GFP_NOFS);
1284 if (!src_blkaddr)
1285 return -ENOMEM;
1286
1287 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1288 array_size(olen, sizeof(int)),
1289 GFP_NOFS);
1290 if (!do_replace) {
1291 kvfree(src_blkaddr);
1292 return -ENOMEM;
1293 }
1294
1295 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1296 do_replace, src, olen);
1297 if (ret)
1298 goto roll_back;
1299
1300 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1301 do_replace, src, dst, olen, full);
1302 if (ret)
1303 goto roll_back;
1304
1305 src += olen;
1306 dst += olen;
1307 len -= olen;
1308
1309 kvfree(src_blkaddr);
1310 kvfree(do_replace);
1311 }
1312 return 0;
1313
1314roll_back:
1315 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1316 kvfree(src_blkaddr);
1317 kvfree(do_replace);
1318 return ret;
1319}
1320
1321static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1322{
1323 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1324 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1325 pgoff_t start = offset >> PAGE_SHIFT;
1326 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1327 int ret;
1328
1329 f2fs_balance_fs(sbi, true);
1330
1331 /* avoid gc operation during block exchange */
1332 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1333 down_write(&F2FS_I(inode)->i_mmap_sem);
1334
1335 f2fs_lock_op(sbi);
1336 f2fs_drop_extent_tree(inode);
1337 truncate_pagecache(inode, offset);
1338 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1339 f2fs_unlock_op(sbi);
1340
1341 up_write(&F2FS_I(inode)->i_mmap_sem);
1342 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1343 return ret;
1344}
1345
1346static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1347{
1348 loff_t new_size;
1349 int ret;
1350
1351 if (offset + len >= i_size_read(inode))
1352 return -EINVAL;
1353
1354 /* collapse range should be aligned to block size of f2fs. */
1355 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1356 return -EINVAL;
1357
1358 ret = f2fs_convert_inline_inode(inode);
1359 if (ret)
1360 return ret;
1361
1362 /* write out all dirty pages from offset */
1363 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1364 if (ret)
1365 return ret;
1366
1367 ret = f2fs_do_collapse(inode, offset, len);
1368 if (ret)
1369 return ret;
1370
1371 /* write out all moved pages, if possible */
1372 down_write(&F2FS_I(inode)->i_mmap_sem);
1373 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1374 truncate_pagecache(inode, offset);
1375
1376 new_size = i_size_read(inode) - len;
1377 ret = f2fs_truncate_blocks(inode, new_size, true);
1378 up_write(&F2FS_I(inode)->i_mmap_sem);
1379 if (!ret)
1380 f2fs_i_size_write(inode, new_size);
1381 return ret;
1382}
1383
1384static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1385 pgoff_t end)
1386{
1387 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1388 pgoff_t index = start;
1389 unsigned int ofs_in_node = dn->ofs_in_node;
1390 blkcnt_t count = 0;
1391 int ret;
1392
1393 for (; index < end; index++, dn->ofs_in_node++) {
1394 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1395 count++;
1396 }
1397
1398 dn->ofs_in_node = ofs_in_node;
1399 ret = f2fs_reserve_new_blocks(dn, count);
1400 if (ret)
1401 return ret;
1402
1403 dn->ofs_in_node = ofs_in_node;
1404 for (index = start; index < end; index++, dn->ofs_in_node++) {
1405 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1406 /*
1407 * f2fs_reserve_new_blocks will not guarantee entire block
1408 * allocation.
1409 */
1410 if (dn->data_blkaddr == NULL_ADDR) {
1411 ret = -ENOSPC;
1412 break;
1413 }
1414 if (dn->data_blkaddr != NEW_ADDR) {
1415 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1416 dn->data_blkaddr = NEW_ADDR;
1417 f2fs_set_data_blkaddr(dn);
1418 }
1419 }
1420
1421 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1422
1423 return ret;
1424}
1425
1426static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1427 int mode)
1428{
1429 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1430 struct address_space *mapping = inode->i_mapping;
1431 pgoff_t index, pg_start, pg_end;
1432 loff_t new_size = i_size_read(inode);
1433 loff_t off_start, off_end;
1434 int ret = 0;
1435
1436 ret = inode_newsize_ok(inode, (len + offset));
1437 if (ret)
1438 return ret;
1439
1440 ret = f2fs_convert_inline_inode(inode);
1441 if (ret)
1442 return ret;
1443
1444 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1445 if (ret)
1446 return ret;
1447
1448 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1449 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1450
1451 off_start = offset & (PAGE_SIZE - 1);
1452 off_end = (offset + len) & (PAGE_SIZE - 1);
1453
1454 if (pg_start == pg_end) {
1455 ret = fill_zero(inode, pg_start, off_start,
1456 off_end - off_start);
1457 if (ret)
1458 return ret;
1459
1460 new_size = max_t(loff_t, new_size, offset + len);
1461 } else {
1462 if (off_start) {
1463 ret = fill_zero(inode, pg_start++, off_start,
1464 PAGE_SIZE - off_start);
1465 if (ret)
1466 return ret;
1467
1468 new_size = max_t(loff_t, new_size,
1469 (loff_t)pg_start << PAGE_SHIFT);
1470 }
1471
1472 for (index = pg_start; index < pg_end;) {
1473 struct dnode_of_data dn;
1474 unsigned int end_offset;
1475 pgoff_t end;
1476
1477 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1478 down_write(&F2FS_I(inode)->i_mmap_sem);
1479
1480 truncate_pagecache_range(inode,
1481 (loff_t)index << PAGE_SHIFT,
1482 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1483
1484 f2fs_lock_op(sbi);
1485
1486 set_new_dnode(&dn, inode, NULL, NULL, 0);
1487 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1488 if (ret) {
1489 f2fs_unlock_op(sbi);
1490 up_write(&F2FS_I(inode)->i_mmap_sem);
1491 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1492 goto out;
1493 }
1494
1495 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1496 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1497
1498 ret = f2fs_do_zero_range(&dn, index, end);
1499 f2fs_put_dnode(&dn);
1500
1501 f2fs_unlock_op(sbi);
1502 up_write(&F2FS_I(inode)->i_mmap_sem);
1503 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1504
1505 f2fs_balance_fs(sbi, dn.node_changed);
1506
1507 if (ret)
1508 goto out;
1509
1510 index = end;
1511 new_size = max_t(loff_t, new_size,
1512 (loff_t)index << PAGE_SHIFT);
1513 }
1514
1515 if (off_end) {
1516 ret = fill_zero(inode, pg_end, 0, off_end);
1517 if (ret)
1518 goto out;
1519
1520 new_size = max_t(loff_t, new_size, offset + len);
1521 }
1522 }
1523
1524out:
1525 if (new_size > i_size_read(inode)) {
1526 if (mode & FALLOC_FL_KEEP_SIZE)
1527 file_set_keep_isize(inode);
1528 else
1529 f2fs_i_size_write(inode, new_size);
1530 }
1531 return ret;
1532}
1533
1534static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1535{
1536 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1537 pgoff_t nr, pg_start, pg_end, delta, idx;
1538 loff_t new_size;
1539 int ret = 0;
1540
1541 new_size = i_size_read(inode) + len;
1542 ret = inode_newsize_ok(inode, new_size);
1543 if (ret)
1544 return ret;
1545
1546 if (offset >= i_size_read(inode))
1547 return -EINVAL;
1548
1549 /* insert range should be aligned to block size of f2fs. */
1550 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1551 return -EINVAL;
1552
1553 ret = f2fs_convert_inline_inode(inode);
1554 if (ret)
1555 return ret;
1556
1557 f2fs_balance_fs(sbi, true);
1558
1559 down_write(&F2FS_I(inode)->i_mmap_sem);
1560 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1561 up_write(&F2FS_I(inode)->i_mmap_sem);
1562 if (ret)
1563 return ret;
1564
1565 /* write out all dirty pages from offset */
1566 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1567 if (ret)
1568 return ret;
1569
1570 pg_start = offset >> PAGE_SHIFT;
1571 pg_end = (offset + len) >> PAGE_SHIFT;
1572 delta = pg_end - pg_start;
1573 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1574
1575 /* avoid gc operation during block exchange */
1576 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1577 down_write(&F2FS_I(inode)->i_mmap_sem);
1578 truncate_pagecache(inode, offset);
1579
1580 while (!ret && idx > pg_start) {
1581 nr = idx - pg_start;
1582 if (nr > delta)
1583 nr = delta;
1584 idx -= nr;
1585
1586 f2fs_lock_op(sbi);
1587 f2fs_drop_extent_tree(inode);
1588
1589 ret = __exchange_data_block(inode, inode, idx,
1590 idx + delta, nr, false);
1591 f2fs_unlock_op(sbi);
1592 }
1593 up_write(&F2FS_I(inode)->i_mmap_sem);
1594 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1595
1596 /* write out all moved pages, if possible */
1597 down_write(&F2FS_I(inode)->i_mmap_sem);
1598 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1599 truncate_pagecache(inode, offset);
1600 up_write(&F2FS_I(inode)->i_mmap_sem);
1601
1602 if (!ret)
1603 f2fs_i_size_write(inode, new_size);
1604 return ret;
1605}
1606
1607static int expand_inode_data(struct inode *inode, loff_t offset,
1608 loff_t len, int mode)
1609{
1610 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1611 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1612 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1613 .m_may_create = true };
1614 pgoff_t pg_end;
1615 loff_t new_size = i_size_read(inode);
1616 loff_t off_end;
1617 int err;
1618
1619 err = inode_newsize_ok(inode, (len + offset));
1620 if (err)
1621 return err;
1622
1623 err = f2fs_convert_inline_inode(inode);
1624 if (err)
1625 return err;
1626
1627 f2fs_balance_fs(sbi, true);
1628
1629 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1630 off_end = (offset + len) & (PAGE_SIZE - 1);
1631
1632 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1633 map.m_len = pg_end - map.m_lblk;
1634 if (off_end)
1635 map.m_len++;
1636
1637 if (!map.m_len)
1638 return 0;
1639
1640 if (f2fs_is_pinned_file(inode)) {
1641 block_t len = (map.m_len >> sbi->log_blocks_per_seg) <<
1642 sbi->log_blocks_per_seg;
1643 block_t done = 0;
1644
1645 if (map.m_len % sbi->blocks_per_seg)
1646 len += sbi->blocks_per_seg;
1647
1648 map.m_len = sbi->blocks_per_seg;
1649next_alloc:
1650 if (has_not_enough_free_secs(sbi, 0,
1651 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1652 down_write(&sbi->gc_lock);
1653 err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1654 if (err && err != -ENODATA && err != -EAGAIN)
1655 goto out_err;
1656 }
1657
1658 down_write(&sbi->pin_sem);
1659 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1660
1661 f2fs_lock_op(sbi);
1662 f2fs_allocate_new_segment(sbi, CURSEG_COLD_DATA);
1663 f2fs_unlock_op(sbi);
1664
1665 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
1666 up_write(&sbi->pin_sem);
1667
1668 done += map.m_len;
1669 len -= map.m_len;
1670 map.m_lblk += map.m_len;
1671 if (!err && len)
1672 goto next_alloc;
1673
1674 map.m_len = done;
1675 } else {
1676 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1677 }
1678out_err:
1679 if (err) {
1680 pgoff_t last_off;
1681
1682 if (!map.m_len)
1683 return err;
1684
1685 last_off = map.m_lblk + map.m_len - 1;
1686
1687 /* update new size to the failed position */
1688 new_size = (last_off == pg_end) ? offset + len :
1689 (loff_t)(last_off + 1) << PAGE_SHIFT;
1690 } else {
1691 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1692 }
1693
1694 if (new_size > i_size_read(inode)) {
1695 if (mode & FALLOC_FL_KEEP_SIZE)
1696 file_set_keep_isize(inode);
1697 else
1698 f2fs_i_size_write(inode, new_size);
1699 }
1700
1701 return err;
1702}
1703
1704static long f2fs_fallocate(struct file *file, int mode,
1705 loff_t offset, loff_t len)
1706{
1707 struct inode *inode = file_inode(file);
1708 long ret = 0;
1709
1710 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1711 return -EIO;
1712 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1713 return -ENOSPC;
1714 if (!f2fs_is_compress_backend_ready(inode))
1715 return -EOPNOTSUPP;
1716
1717 /* f2fs only support ->fallocate for regular file */
1718 if (!S_ISREG(inode->i_mode))
1719 return -EINVAL;
1720
1721 if (IS_ENCRYPTED(inode) &&
1722 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1723 return -EOPNOTSUPP;
1724
1725 if (f2fs_compressed_file(inode) &&
1726 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1727 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
1728 return -EOPNOTSUPP;
1729
1730 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1731 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1732 FALLOC_FL_INSERT_RANGE))
1733 return -EOPNOTSUPP;
1734
1735 inode_lock(inode);
1736
1737 if (mode & FALLOC_FL_PUNCH_HOLE) {
1738 if (offset >= inode->i_size)
1739 goto out;
1740
1741 ret = punch_hole(inode, offset, len);
1742 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1743 ret = f2fs_collapse_range(inode, offset, len);
1744 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1745 ret = f2fs_zero_range(inode, offset, len, mode);
1746 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1747 ret = f2fs_insert_range(inode, offset, len);
1748 } else {
1749 ret = expand_inode_data(inode, offset, len, mode);
1750 }
1751
1752 if (!ret) {
1753 inode->i_mtime = inode->i_ctime = current_time(inode);
1754 f2fs_mark_inode_dirty_sync(inode, false);
1755 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1756 }
1757
1758out:
1759 inode_unlock(inode);
1760
1761 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1762 return ret;
1763}
1764
1765static int f2fs_release_file(struct inode *inode, struct file *filp)
1766{
1767 /*
1768 * f2fs_relase_file is called at every close calls. So we should
1769 * not drop any inmemory pages by close called by other process.
1770 */
1771 if (!(filp->f_mode & FMODE_WRITE) ||
1772 atomic_read(&inode->i_writecount) != 1)
1773 return 0;
1774
1775 /* some remained atomic pages should discarded */
1776 if (f2fs_is_atomic_file(inode))
1777 f2fs_drop_inmem_pages(inode);
1778 if (f2fs_is_volatile_file(inode)) {
1779 set_inode_flag(inode, FI_DROP_CACHE);
1780 filemap_fdatawrite(inode->i_mapping);
1781 clear_inode_flag(inode, FI_DROP_CACHE);
1782 clear_inode_flag(inode, FI_VOLATILE_FILE);
1783 stat_dec_volatile_write(inode);
1784 }
1785 return 0;
1786}
1787
1788static int f2fs_file_flush(struct file *file, fl_owner_t id)
1789{
1790 struct inode *inode = file_inode(file);
1791
1792 /*
1793 * If the process doing a transaction is crashed, we should do
1794 * roll-back. Otherwise, other reader/write can see corrupted database
1795 * until all the writers close its file. Since this should be done
1796 * before dropping file lock, it needs to do in ->flush.
1797 */
1798 if (f2fs_is_atomic_file(inode) &&
1799 F2FS_I(inode)->inmem_task == current)
1800 f2fs_drop_inmem_pages(inode);
1801 return 0;
1802}
1803
1804static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1805{
1806 struct f2fs_inode_info *fi = F2FS_I(inode);
1807 u32 masked_flags = fi->i_flags & mask;
1808
1809 f2fs_bug_on(F2FS_I_SB(inode), (iflags & ~mask));
1810
1811 /* Is it quota file? Do not allow user to mess with it */
1812 if (IS_NOQUOTA(inode))
1813 return -EPERM;
1814
1815 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
1816 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1817 return -EOPNOTSUPP;
1818 if (!f2fs_empty_dir(inode))
1819 return -ENOTEMPTY;
1820 }
1821
1822 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
1823 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
1824 return -EOPNOTSUPP;
1825 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
1826 return -EINVAL;
1827 }
1828
1829 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
1830 if (masked_flags & F2FS_COMPR_FL) {
1831 if (f2fs_disable_compressed_file(inode))
1832 return -EINVAL;
1833 }
1834 if (iflags & F2FS_NOCOMP_FL)
1835 return -EINVAL;
1836 if (iflags & F2FS_COMPR_FL) {
1837 if (!f2fs_may_compress(inode))
1838 return -EINVAL;
1839
1840 set_compress_context(inode);
1841 }
1842 }
1843 if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) {
1844 if (masked_flags & F2FS_COMPR_FL)
1845 return -EINVAL;
1846 }
1847
1848 fi->i_flags = iflags | (fi->i_flags & ~mask);
1849 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
1850 (fi->i_flags & F2FS_NOCOMP_FL));
1851
1852 if (fi->i_flags & F2FS_PROJINHERIT_FL)
1853 set_inode_flag(inode, FI_PROJ_INHERIT);
1854 else
1855 clear_inode_flag(inode, FI_PROJ_INHERIT);
1856
1857 inode->i_ctime = current_time(inode);
1858 f2fs_set_inode_flags(inode);
1859 f2fs_mark_inode_dirty_sync(inode, true);
1860 return 0;
1861}
1862
1863/* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */
1864
1865/*
1866 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
1867 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
1868 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
1869 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
1870 */
1871
1872static const struct {
1873 u32 iflag;
1874 u32 fsflag;
1875} f2fs_fsflags_map[] = {
1876 { F2FS_COMPR_FL, FS_COMPR_FL },
1877 { F2FS_SYNC_FL, FS_SYNC_FL },
1878 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
1879 { F2FS_APPEND_FL, FS_APPEND_FL },
1880 { F2FS_NODUMP_FL, FS_NODUMP_FL },
1881 { F2FS_NOATIME_FL, FS_NOATIME_FL },
1882 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
1883 { F2FS_INDEX_FL, FS_INDEX_FL },
1884 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
1885 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1886 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
1887};
1888
1889#define F2FS_GETTABLE_FS_FL ( \
1890 FS_COMPR_FL | \
1891 FS_SYNC_FL | \
1892 FS_IMMUTABLE_FL | \
1893 FS_APPEND_FL | \
1894 FS_NODUMP_FL | \
1895 FS_NOATIME_FL | \
1896 FS_NOCOMP_FL | \
1897 FS_INDEX_FL | \
1898 FS_DIRSYNC_FL | \
1899 FS_PROJINHERIT_FL | \
1900 FS_ENCRYPT_FL | \
1901 FS_INLINE_DATA_FL | \
1902 FS_NOCOW_FL | \
1903 FS_VERITY_FL | \
1904 FS_CASEFOLD_FL)
1905
1906#define F2FS_SETTABLE_FS_FL ( \
1907 FS_COMPR_FL | \
1908 FS_SYNC_FL | \
1909 FS_IMMUTABLE_FL | \
1910 FS_APPEND_FL | \
1911 FS_NODUMP_FL | \
1912 FS_NOATIME_FL | \
1913 FS_NOCOMP_FL | \
1914 FS_DIRSYNC_FL | \
1915 FS_PROJINHERIT_FL | \
1916 FS_CASEFOLD_FL)
1917
1918/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
1919static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
1920{
1921 u32 fsflags = 0;
1922 int i;
1923
1924 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1925 if (iflags & f2fs_fsflags_map[i].iflag)
1926 fsflags |= f2fs_fsflags_map[i].fsflag;
1927
1928 return fsflags;
1929}
1930
1931/* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
1932static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
1933{
1934 u32 iflags = 0;
1935 int i;
1936
1937 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
1938 if (fsflags & f2fs_fsflags_map[i].fsflag)
1939 iflags |= f2fs_fsflags_map[i].iflag;
1940
1941 return iflags;
1942}
1943
1944static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1945{
1946 struct inode *inode = file_inode(filp);
1947 struct f2fs_inode_info *fi = F2FS_I(inode);
1948 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1949
1950 if (IS_ENCRYPTED(inode))
1951 fsflags |= FS_ENCRYPT_FL;
1952 if (IS_VERITY(inode))
1953 fsflags |= FS_VERITY_FL;
1954 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
1955 fsflags |= FS_INLINE_DATA_FL;
1956 if (is_inode_flag_set(inode, FI_PIN_FILE))
1957 fsflags |= FS_NOCOW_FL;
1958
1959 fsflags &= F2FS_GETTABLE_FS_FL;
1960
1961 return put_user(fsflags, (int __user *)arg);
1962}
1963
1964static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1965{
1966 struct inode *inode = file_inode(filp);
1967 struct f2fs_inode_info *fi = F2FS_I(inode);
1968 u32 fsflags, old_fsflags;
1969 u32 iflags;
1970 int ret;
1971
1972 if (!inode_owner_or_capable(inode))
1973 return -EACCES;
1974
1975 if (get_user(fsflags, (int __user *)arg))
1976 return -EFAULT;
1977
1978 if (fsflags & ~F2FS_GETTABLE_FS_FL)
1979 return -EOPNOTSUPP;
1980 fsflags &= F2FS_SETTABLE_FS_FL;
1981
1982 iflags = f2fs_fsflags_to_iflags(fsflags);
1983 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
1984 return -EOPNOTSUPP;
1985
1986 ret = mnt_want_write_file(filp);
1987 if (ret)
1988 return ret;
1989
1990 inode_lock(inode);
1991
1992 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
1993 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
1994 if (ret)
1995 goto out;
1996
1997 ret = f2fs_setflags_common(inode, iflags,
1998 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL));
1999out:
2000 inode_unlock(inode);
2001 mnt_drop_write_file(filp);
2002 return ret;
2003}
2004
2005static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2006{
2007 struct inode *inode = file_inode(filp);
2008
2009 return put_user(inode->i_generation, (int __user *)arg);
2010}
2011
2012static int f2fs_ioc_start_atomic_write(struct file *filp)
2013{
2014 struct inode *inode = file_inode(filp);
2015 struct f2fs_inode_info *fi = F2FS_I(inode);
2016 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2017 int ret;
2018
2019 if (!inode_owner_or_capable(inode))
2020 return -EACCES;
2021
2022 if (!S_ISREG(inode->i_mode))
2023 return -EINVAL;
2024
2025 if (filp->f_flags & O_DIRECT)
2026 return -EINVAL;
2027
2028 ret = mnt_want_write_file(filp);
2029 if (ret)
2030 return ret;
2031
2032 inode_lock(inode);
2033
2034 f2fs_disable_compressed_file(inode);
2035
2036 if (f2fs_is_atomic_file(inode)) {
2037 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
2038 ret = -EINVAL;
2039 goto out;
2040 }
2041
2042 ret = f2fs_convert_inline_inode(inode);
2043 if (ret)
2044 goto out;
2045
2046 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2047
2048 /*
2049 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2050 * f2fs_is_atomic_file.
2051 */
2052 if (get_dirty_pages(inode))
2053 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2054 inode->i_ino, get_dirty_pages(inode));
2055 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2056 if (ret) {
2057 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2058 goto out;
2059 }
2060
2061 spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
2062 if (list_empty(&fi->inmem_ilist))
2063 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
2064 sbi->atomic_files++;
2065 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
2066
2067 /* add inode in inmem_list first and set atomic_file */
2068 set_inode_flag(inode, FI_ATOMIC_FILE);
2069 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2070 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2071
2072 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2073 F2FS_I(inode)->inmem_task = current;
2074 stat_update_max_atomic_write(inode);
2075out:
2076 inode_unlock(inode);
2077 mnt_drop_write_file(filp);
2078 return ret;
2079}
2080
2081static int f2fs_ioc_commit_atomic_write(struct file *filp)
2082{
2083 struct inode *inode = file_inode(filp);
2084 int ret;
2085
2086 if (!inode_owner_or_capable(inode))
2087 return -EACCES;
2088
2089 ret = mnt_want_write_file(filp);
2090 if (ret)
2091 return ret;
2092
2093 f2fs_balance_fs(F2FS_I_SB(inode), true);
2094
2095 inode_lock(inode);
2096
2097 if (f2fs_is_volatile_file(inode)) {
2098 ret = -EINVAL;
2099 goto err_out;
2100 }
2101
2102 if (f2fs_is_atomic_file(inode)) {
2103 ret = f2fs_commit_inmem_pages(inode);
2104 if (ret)
2105 goto err_out;
2106
2107 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2108 if (!ret)
2109 f2fs_drop_inmem_pages(inode);
2110 } else {
2111 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2112 }
2113err_out:
2114 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2115 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2116 ret = -EINVAL;
2117 }
2118 inode_unlock(inode);
2119 mnt_drop_write_file(filp);
2120 return ret;
2121}
2122
2123static int f2fs_ioc_start_volatile_write(struct file *filp)
2124{
2125 struct inode *inode = file_inode(filp);
2126 int ret;
2127
2128 if (!inode_owner_or_capable(inode))
2129 return -EACCES;
2130
2131 if (!S_ISREG(inode->i_mode))
2132 return -EINVAL;
2133
2134 ret = mnt_want_write_file(filp);
2135 if (ret)
2136 return ret;
2137
2138 inode_lock(inode);
2139
2140 if (f2fs_is_volatile_file(inode))
2141 goto out;
2142
2143 ret = f2fs_convert_inline_inode(inode);
2144 if (ret)
2145 goto out;
2146
2147 stat_inc_volatile_write(inode);
2148 stat_update_max_volatile_write(inode);
2149
2150 set_inode_flag(inode, FI_VOLATILE_FILE);
2151 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2152out:
2153 inode_unlock(inode);
2154 mnt_drop_write_file(filp);
2155 return ret;
2156}
2157
2158static int f2fs_ioc_release_volatile_write(struct file *filp)
2159{
2160 struct inode *inode = file_inode(filp);
2161 int ret;
2162
2163 if (!inode_owner_or_capable(inode))
2164 return -EACCES;
2165
2166 ret = mnt_want_write_file(filp);
2167 if (ret)
2168 return ret;
2169
2170 inode_lock(inode);
2171
2172 if (!f2fs_is_volatile_file(inode))
2173 goto out;
2174
2175 if (!f2fs_is_first_block_written(inode)) {
2176 ret = truncate_partial_data_page(inode, 0, true);
2177 goto out;
2178 }
2179
2180 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
2181out:
2182 inode_unlock(inode);
2183 mnt_drop_write_file(filp);
2184 return ret;
2185}
2186
2187static int f2fs_ioc_abort_volatile_write(struct file *filp)
2188{
2189 struct inode *inode = file_inode(filp);
2190 int ret;
2191
2192 if (!inode_owner_or_capable(inode))
2193 return -EACCES;
2194
2195 ret = mnt_want_write_file(filp);
2196 if (ret)
2197 return ret;
2198
2199 inode_lock(inode);
2200
2201 if (f2fs_is_atomic_file(inode))
2202 f2fs_drop_inmem_pages(inode);
2203 if (f2fs_is_volatile_file(inode)) {
2204 clear_inode_flag(inode, FI_VOLATILE_FILE);
2205 stat_dec_volatile_write(inode);
2206 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2207 }
2208
2209 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
2210
2211 inode_unlock(inode);
2212
2213 mnt_drop_write_file(filp);
2214 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2215 return ret;
2216}
2217
2218static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2219{
2220 struct inode *inode = file_inode(filp);
2221 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2222 struct super_block *sb = sbi->sb;
2223 __u32 in;
2224 int ret = 0;
2225
2226 if (!capable(CAP_SYS_ADMIN))
2227 return -EPERM;
2228
2229 if (get_user(in, (__u32 __user *)arg))
2230 return -EFAULT;
2231
2232 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2233 ret = mnt_want_write_file(filp);
2234 if (ret) {
2235 if (ret == -EROFS) {
2236 ret = 0;
2237 f2fs_stop_checkpoint(sbi, false);
2238 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2239 trace_f2fs_shutdown(sbi, in, ret);
2240 }
2241 return ret;
2242 }
2243 }
2244
2245 switch (in) {
2246 case F2FS_GOING_DOWN_FULLSYNC:
2247 sb = freeze_bdev(sb->s_bdev);
2248 if (IS_ERR(sb)) {
2249 ret = PTR_ERR(sb);
2250 goto out;
2251 }
2252 if (sb) {
2253 f2fs_stop_checkpoint(sbi, false);
2254 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2255 thaw_bdev(sb->s_bdev, sb);
2256 }
2257 break;
2258 case F2FS_GOING_DOWN_METASYNC:
2259 /* do checkpoint only */
2260 ret = f2fs_sync_fs(sb, 1);
2261 if (ret)
2262 goto out;
2263 f2fs_stop_checkpoint(sbi, false);
2264 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2265 break;
2266 case F2FS_GOING_DOWN_NOSYNC:
2267 f2fs_stop_checkpoint(sbi, false);
2268 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2269 break;
2270 case F2FS_GOING_DOWN_METAFLUSH:
2271 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2272 f2fs_stop_checkpoint(sbi, false);
2273 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
2274 break;
2275 case F2FS_GOING_DOWN_NEED_FSCK:
2276 set_sbi_flag(sbi, SBI_NEED_FSCK);
2277 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2278 set_sbi_flag(sbi, SBI_IS_DIRTY);
2279 /* do checkpoint only */
2280 ret = f2fs_sync_fs(sb, 1);
2281 goto out;
2282 default:
2283 ret = -EINVAL;
2284 goto out;
2285 }
2286
2287 f2fs_stop_gc_thread(sbi);
2288 f2fs_stop_discard_thread(sbi);
2289
2290 f2fs_drop_discard_cmd(sbi);
2291 clear_opt(sbi, DISCARD);
2292
2293 f2fs_update_time(sbi, REQ_TIME);
2294out:
2295 if (in != F2FS_GOING_DOWN_FULLSYNC)
2296 mnt_drop_write_file(filp);
2297
2298 trace_f2fs_shutdown(sbi, in, ret);
2299
2300 return ret;
2301}
2302
2303static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2304{
2305 struct inode *inode = file_inode(filp);
2306 struct super_block *sb = inode->i_sb;
2307 struct request_queue *q = bdev_get_queue(sb->s_bdev);
2308 struct fstrim_range range;
2309 int ret;
2310
2311 if (!capable(CAP_SYS_ADMIN))
2312 return -EPERM;
2313
2314 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2315 return -EOPNOTSUPP;
2316
2317 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2318 sizeof(range)))
2319 return -EFAULT;
2320
2321 ret = mnt_want_write_file(filp);
2322 if (ret)
2323 return ret;
2324
2325 range.minlen = max((unsigned int)range.minlen,
2326 q->limits.discard_granularity);
2327 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2328 mnt_drop_write_file(filp);
2329 if (ret < 0)
2330 return ret;
2331
2332 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2333 sizeof(range)))
2334 return -EFAULT;
2335 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2336 return 0;
2337}
2338
2339static bool uuid_is_nonzero(__u8 u[16])
2340{
2341 int i;
2342
2343 for (i = 0; i < 16; i++)
2344 if (u[i])
2345 return true;
2346 return false;
2347}
2348
2349static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2350{
2351 struct inode *inode = file_inode(filp);
2352
2353 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2354 return -EOPNOTSUPP;
2355
2356 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2357
2358 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2359}
2360
2361static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2362{
2363 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2364 return -EOPNOTSUPP;
2365 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2366}
2367
2368static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2369{
2370 struct inode *inode = file_inode(filp);
2371 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2372 int err;
2373
2374 if (!f2fs_sb_has_encrypt(sbi))
2375 return -EOPNOTSUPP;
2376
2377 err = mnt_want_write_file(filp);
2378 if (err)
2379 return err;
2380
2381 down_write(&sbi->sb_lock);
2382
2383 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2384 goto got_it;
2385
2386 /* update superblock with uuid */
2387 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2388
2389 err = f2fs_commit_super(sbi, false);
2390 if (err) {
2391 /* undo new data */
2392 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2393 goto out_err;
2394 }
2395got_it:
2396 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
2397 16))
2398 err = -EFAULT;
2399out_err:
2400 up_write(&sbi->sb_lock);
2401 mnt_drop_write_file(filp);
2402 return err;
2403}
2404
2405static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2406 unsigned long arg)
2407{
2408 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2409 return -EOPNOTSUPP;
2410
2411 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2412}
2413
2414static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2415{
2416 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2417 return -EOPNOTSUPP;
2418
2419 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2420}
2421
2422static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2423{
2424 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2425 return -EOPNOTSUPP;
2426
2427 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2428}
2429
2430static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2431 unsigned long arg)
2432{
2433 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2434 return -EOPNOTSUPP;
2435
2436 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2437}
2438
2439static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2440 unsigned long arg)
2441{
2442 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2443 return -EOPNOTSUPP;
2444
2445 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2446}
2447
2448static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2449{
2450 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2451 return -EOPNOTSUPP;
2452
2453 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2454}
2455
2456static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2457{
2458 struct inode *inode = file_inode(filp);
2459 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2460 __u32 sync;
2461 int ret;
2462
2463 if (!capable(CAP_SYS_ADMIN))
2464 return -EPERM;
2465
2466 if (get_user(sync, (__u32 __user *)arg))
2467 return -EFAULT;
2468
2469 if (f2fs_readonly(sbi->sb))
2470 return -EROFS;
2471
2472 ret = mnt_want_write_file(filp);
2473 if (ret)
2474 return ret;
2475
2476 if (!sync) {
2477 if (!down_write_trylock(&sbi->gc_lock)) {
2478 ret = -EBUSY;
2479 goto out;
2480 }
2481 } else {
2482 down_write(&sbi->gc_lock);
2483 }
2484
2485 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
2486out:
2487 mnt_drop_write_file(filp);
2488 return ret;
2489}
2490
2491static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2492{
2493 struct inode *inode = file_inode(filp);
2494 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2495 struct f2fs_gc_range range;
2496 u64 end;
2497 int ret;
2498
2499 if (!capable(CAP_SYS_ADMIN))
2500 return -EPERM;
2501
2502 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2503 sizeof(range)))
2504 return -EFAULT;
2505
2506 if (f2fs_readonly(sbi->sb))
2507 return -EROFS;
2508
2509 end = range.start + range.len;
2510 if (end < range.start || range.start < MAIN_BLKADDR(sbi) ||
2511 end >= MAX_BLKADDR(sbi))
2512 return -EINVAL;
2513
2514 ret = mnt_want_write_file(filp);
2515 if (ret)
2516 return ret;
2517
2518do_more:
2519 if (!range.sync) {
2520 if (!down_write_trylock(&sbi->gc_lock)) {
2521 ret = -EBUSY;
2522 goto out;
2523 }
2524 } else {
2525 down_write(&sbi->gc_lock);
2526 }
2527
2528 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2529 if (ret) {
2530 if (ret == -EBUSY)
2531 ret = -EAGAIN;
2532 goto out;
2533 }
2534 range.start += BLKS_PER_SEC(sbi);
2535 if (range.start <= end)
2536 goto do_more;
2537out:
2538 mnt_drop_write_file(filp);
2539 return ret;
2540}
2541
2542static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2543{
2544 struct inode *inode = file_inode(filp);
2545 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2546 int ret;
2547
2548 if (!capable(CAP_SYS_ADMIN))
2549 return -EPERM;
2550
2551 if (f2fs_readonly(sbi->sb))
2552 return -EROFS;
2553
2554 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2555 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2556 return -EINVAL;
2557 }
2558
2559 ret = mnt_want_write_file(filp);
2560 if (ret)
2561 return ret;
2562
2563 ret = f2fs_sync_fs(sbi->sb, 1);
2564
2565 mnt_drop_write_file(filp);
2566 return ret;
2567}
2568
2569static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2570 struct file *filp,
2571 struct f2fs_defragment *range)
2572{
2573 struct inode *inode = file_inode(filp);
2574 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2575 .m_seg_type = NO_CHECK_TYPE ,
2576 .m_may_create = false };
2577 struct extent_info ei = {0, 0, 0};
2578 pgoff_t pg_start, pg_end, next_pgofs;
2579 unsigned int blk_per_seg = sbi->blocks_per_seg;
2580 unsigned int total = 0, sec_num;
2581 block_t blk_end = 0;
2582 bool fragmented = false;
2583 int err;
2584
2585 /* if in-place-update policy is enabled, don't waste time here */
2586 if (f2fs_should_update_inplace(inode, NULL))
2587 return -EINVAL;
2588
2589 pg_start = range->start >> PAGE_SHIFT;
2590 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2591
2592 f2fs_balance_fs(sbi, true);
2593
2594 inode_lock(inode);
2595
2596 /* writeback all dirty pages in the range */
2597 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2598 range->start + range->len - 1);
2599 if (err)
2600 goto out;
2601
2602 /*
2603 * lookup mapping info in extent cache, skip defragmenting if physical
2604 * block addresses are continuous.
2605 */
2606 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2607 if (ei.fofs + ei.len >= pg_end)
2608 goto out;
2609 }
2610
2611 map.m_lblk = pg_start;
2612 map.m_next_pgofs = &next_pgofs;
2613
2614 /*
2615 * lookup mapping info in dnode page cache, skip defragmenting if all
2616 * physical block addresses are continuous even if there are hole(s)
2617 * in logical blocks.
2618 */
2619 while (map.m_lblk < pg_end) {
2620 map.m_len = pg_end - map.m_lblk;
2621 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2622 if (err)
2623 goto out;
2624
2625 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2626 map.m_lblk = next_pgofs;
2627 continue;
2628 }
2629
2630 if (blk_end && blk_end != map.m_pblk)
2631 fragmented = true;
2632
2633 /* record total count of block that we're going to move */
2634 total += map.m_len;
2635
2636 blk_end = map.m_pblk + map.m_len;
2637
2638 map.m_lblk += map.m_len;
2639 }
2640
2641 if (!fragmented) {
2642 total = 0;
2643 goto out;
2644 }
2645
2646 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi));
2647
2648 /*
2649 * make sure there are enough free section for LFS allocation, this can
2650 * avoid defragment running in SSR mode when free section are allocated
2651 * intensively
2652 */
2653 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2654 err = -EAGAIN;
2655 goto out;
2656 }
2657
2658 map.m_lblk = pg_start;
2659 map.m_len = pg_end - pg_start;
2660 total = 0;
2661
2662 while (map.m_lblk < pg_end) {
2663 pgoff_t idx;
2664 int cnt = 0;
2665
2666do_map:
2667 map.m_len = pg_end - map.m_lblk;
2668 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2669 if (err)
2670 goto clear_out;
2671
2672 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2673 map.m_lblk = next_pgofs;
2674 goto check;
2675 }
2676
2677 set_inode_flag(inode, FI_DO_DEFRAG);
2678
2679 idx = map.m_lblk;
2680 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2681 struct page *page;
2682
2683 page = f2fs_get_lock_data_page(inode, idx, true);
2684 if (IS_ERR(page)) {
2685 err = PTR_ERR(page);
2686 goto clear_out;
2687 }
2688
2689 set_page_dirty(page);
2690 f2fs_put_page(page, 1);
2691
2692 idx++;
2693 cnt++;
2694 total++;
2695 }
2696
2697 map.m_lblk = idx;
2698check:
2699 if (map.m_lblk < pg_end && cnt < blk_per_seg)
2700 goto do_map;
2701
2702 clear_inode_flag(inode, FI_DO_DEFRAG);
2703
2704 err = filemap_fdatawrite(inode->i_mapping);
2705 if (err)
2706 goto out;
2707 }
2708clear_out:
2709 clear_inode_flag(inode, FI_DO_DEFRAG);
2710out:
2711 inode_unlock(inode);
2712 if (!err)
2713 range->len = (u64)total << PAGE_SHIFT;
2714 return err;
2715}
2716
2717static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2718{
2719 struct inode *inode = file_inode(filp);
2720 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2721 struct f2fs_defragment range;
2722 int err;
2723
2724 if (!capable(CAP_SYS_ADMIN))
2725 return -EPERM;
2726
2727 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2728 return -EINVAL;
2729
2730 if (f2fs_readonly(sbi->sb))
2731 return -EROFS;
2732
2733 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2734 sizeof(range)))
2735 return -EFAULT;
2736
2737 /* verify alignment of offset & size */
2738 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2739 return -EINVAL;
2740
2741 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2742 sbi->max_file_blocks))
2743 return -EINVAL;
2744
2745 err = mnt_want_write_file(filp);
2746 if (err)
2747 return err;
2748
2749 err = f2fs_defragment_range(sbi, filp, &range);
2750 mnt_drop_write_file(filp);
2751
2752 f2fs_update_time(sbi, REQ_TIME);
2753 if (err < 0)
2754 return err;
2755
2756 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2757 sizeof(range)))
2758 return -EFAULT;
2759
2760 return 0;
2761}
2762
2763static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2764 struct file *file_out, loff_t pos_out, size_t len)
2765{
2766 struct inode *src = file_inode(file_in);
2767 struct inode *dst = file_inode(file_out);
2768 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2769 size_t olen = len, dst_max_i_size = 0;
2770 size_t dst_osize;
2771 int ret;
2772
2773 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2774 src->i_sb != dst->i_sb)
2775 return -EXDEV;
2776
2777 if (unlikely(f2fs_readonly(src->i_sb)))
2778 return -EROFS;
2779
2780 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2781 return -EINVAL;
2782
2783 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2784 return -EOPNOTSUPP;
2785
2786 if (src == dst) {
2787 if (pos_in == pos_out)
2788 return 0;
2789 if (pos_out > pos_in && pos_out < pos_in + len)
2790 return -EINVAL;
2791 }
2792
2793 inode_lock(src);
2794 if (src != dst) {
2795 ret = -EBUSY;
2796 if (!inode_trylock(dst))
2797 goto out;
2798 }
2799
2800 ret = -EINVAL;
2801 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2802 goto out_unlock;
2803 if (len == 0)
2804 olen = len = src->i_size - pos_in;
2805 if (pos_in + len == src->i_size)
2806 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2807 if (len == 0) {
2808 ret = 0;
2809 goto out_unlock;
2810 }
2811
2812 dst_osize = dst->i_size;
2813 if (pos_out + olen > dst->i_size)
2814 dst_max_i_size = pos_out + olen;
2815
2816 /* verify the end result is block aligned */
2817 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2818 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2819 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2820 goto out_unlock;
2821
2822 ret = f2fs_convert_inline_inode(src);
2823 if (ret)
2824 goto out_unlock;
2825
2826 ret = f2fs_convert_inline_inode(dst);
2827 if (ret)
2828 goto out_unlock;
2829
2830 /* write out all dirty pages from offset */
2831 ret = filemap_write_and_wait_range(src->i_mapping,
2832 pos_in, pos_in + len);
2833 if (ret)
2834 goto out_unlock;
2835
2836 ret = filemap_write_and_wait_range(dst->i_mapping,
2837 pos_out, pos_out + len);
2838 if (ret)
2839 goto out_unlock;
2840
2841 f2fs_balance_fs(sbi, true);
2842
2843 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2844 if (src != dst) {
2845 ret = -EBUSY;
2846 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
2847 goto out_src;
2848 }
2849
2850 f2fs_lock_op(sbi);
2851 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2852 pos_out >> F2FS_BLKSIZE_BITS,
2853 len >> F2FS_BLKSIZE_BITS, false);
2854
2855 if (!ret) {
2856 if (dst_max_i_size)
2857 f2fs_i_size_write(dst, dst_max_i_size);
2858 else if (dst_osize != dst->i_size)
2859 f2fs_i_size_write(dst, dst_osize);
2860 }
2861 f2fs_unlock_op(sbi);
2862
2863 if (src != dst)
2864 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
2865out_src:
2866 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
2867out_unlock:
2868 if (src != dst)
2869 inode_unlock(dst);
2870out:
2871 inode_unlock(src);
2872 return ret;
2873}
2874
2875static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2876{
2877 struct f2fs_move_range range;
2878 struct fd dst;
2879 int err;
2880
2881 if (!(filp->f_mode & FMODE_READ) ||
2882 !(filp->f_mode & FMODE_WRITE))
2883 return -EBADF;
2884
2885 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2886 sizeof(range)))
2887 return -EFAULT;
2888
2889 dst = fdget(range.dst_fd);
2890 if (!dst.file)
2891 return -EBADF;
2892
2893 if (!(dst.file->f_mode & FMODE_WRITE)) {
2894 err = -EBADF;
2895 goto err_out;
2896 }
2897
2898 err = mnt_want_write_file(filp);
2899 if (err)
2900 goto err_out;
2901
2902 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2903 range.pos_out, range.len);
2904
2905 mnt_drop_write_file(filp);
2906 if (err)
2907 goto err_out;
2908
2909 if (copy_to_user((struct f2fs_move_range __user *)arg,
2910 &range, sizeof(range)))
2911 err = -EFAULT;
2912err_out:
2913 fdput(dst);
2914 return err;
2915}
2916
2917static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2918{
2919 struct inode *inode = file_inode(filp);
2920 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2921 struct sit_info *sm = SIT_I(sbi);
2922 unsigned int start_segno = 0, end_segno = 0;
2923 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2924 struct f2fs_flush_device range;
2925 int ret;
2926
2927 if (!capable(CAP_SYS_ADMIN))
2928 return -EPERM;
2929
2930 if (f2fs_readonly(sbi->sb))
2931 return -EROFS;
2932
2933 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2934 return -EINVAL;
2935
2936 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2937 sizeof(range)))
2938 return -EFAULT;
2939
2940 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
2941 __is_large_section(sbi)) {
2942 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1",
2943 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);
2944 return -EINVAL;
2945 }
2946
2947 ret = mnt_want_write_file(filp);
2948 if (ret)
2949 return ret;
2950
2951 if (range.dev_num != 0)
2952 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2953 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2954
2955 start_segno = sm->last_victim[FLUSH_DEVICE];
2956 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2957 start_segno = dev_start_segno;
2958 end_segno = min(start_segno + range.segments, dev_end_segno);
2959
2960 while (start_segno < end_segno) {
2961 if (!down_write_trylock(&sbi->gc_lock)) {
2962 ret = -EBUSY;
2963 goto out;
2964 }
2965 sm->last_victim[GC_CB] = end_segno + 1;
2966 sm->last_victim[GC_GREEDY] = end_segno + 1;
2967 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2968 ret = f2fs_gc(sbi, true, true, start_segno);
2969 if (ret == -EAGAIN)
2970 ret = 0;
2971 else if (ret < 0)
2972 break;
2973 start_segno++;
2974 }
2975out:
2976 mnt_drop_write_file(filp);
2977 return ret;
2978}
2979
2980static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2981{
2982 struct inode *inode = file_inode(filp);
2983 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2984
2985 /* Must validate to set it with SQLite behavior in Android. */
2986 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2987
2988 return put_user(sb_feature, (u32 __user *)arg);
2989}
2990
2991#ifdef CONFIG_QUOTA
2992int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
2993{
2994 struct dquot *transfer_to[MAXQUOTAS] = {};
2995 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2996 struct super_block *sb = sbi->sb;
2997 int err = 0;
2998
2999 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3000 if (!IS_ERR(transfer_to[PRJQUOTA])) {
3001 err = __dquot_transfer(inode, transfer_to);
3002 if (err)
3003 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3004 dqput(transfer_to[PRJQUOTA]);
3005 }
3006 return err;
3007}
3008
3009static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3010{
3011 struct inode *inode = file_inode(filp);
3012 struct f2fs_inode_info *fi = F2FS_I(inode);
3013 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3014 struct page *ipage;
3015 kprojid_t kprojid;
3016 int err;
3017
3018 if (!f2fs_sb_has_project_quota(sbi)) {
3019 if (projid != F2FS_DEF_PROJID)
3020 return -EOPNOTSUPP;
3021 else
3022 return 0;
3023 }
3024
3025 if (!f2fs_has_extra_attr(inode))
3026 return -EOPNOTSUPP;
3027
3028 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3029
3030 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
3031 return 0;
3032
3033 err = -EPERM;
3034 /* Is it quota file? Do not allow user to mess with it */
3035 if (IS_NOQUOTA(inode))
3036 return err;
3037
3038 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3039 if (IS_ERR(ipage))
3040 return PTR_ERR(ipage);
3041
3042 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
3043 i_projid)) {
3044 err = -EOVERFLOW;
3045 f2fs_put_page(ipage, 1);
3046 return err;
3047 }
3048 f2fs_put_page(ipage, 1);
3049
3050 err = dquot_initialize(inode);
3051 if (err)
3052 return err;
3053
3054 f2fs_lock_op(sbi);
3055 err = f2fs_transfer_project_quota(inode, kprojid);
3056 if (err)
3057 goto out_unlock;
3058
3059 F2FS_I(inode)->i_projid = kprojid;
3060 inode->i_ctime = current_time(inode);
3061 f2fs_mark_inode_dirty_sync(inode, true);
3062out_unlock:
3063 f2fs_unlock_op(sbi);
3064 return err;
3065}
3066#else
3067int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3068{
3069 return 0;
3070}
3071
3072static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
3073{
3074 if (projid != F2FS_DEF_PROJID)
3075 return -EOPNOTSUPP;
3076 return 0;
3077}
3078#endif
3079
3080/* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */
3081
3082/*
3083 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable
3084 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its
3085 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS.
3086 */
3087
3088static const struct {
3089 u32 iflag;
3090 u32 xflag;
3091} f2fs_xflags_map[] = {
3092 { F2FS_SYNC_FL, FS_XFLAG_SYNC },
3093 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE },
3094 { F2FS_APPEND_FL, FS_XFLAG_APPEND },
3095 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP },
3096 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME },
3097 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT },
3098};
3099
3100#define F2FS_SUPPORTED_XFLAGS ( \
3101 FS_XFLAG_SYNC | \
3102 FS_XFLAG_IMMUTABLE | \
3103 FS_XFLAG_APPEND | \
3104 FS_XFLAG_NODUMP | \
3105 FS_XFLAG_NOATIME | \
3106 FS_XFLAG_PROJINHERIT)
3107
3108/* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */
3109static inline u32 f2fs_iflags_to_xflags(u32 iflags)
3110{
3111 u32 xflags = 0;
3112 int i;
3113
3114 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3115 if (iflags & f2fs_xflags_map[i].iflag)
3116 xflags |= f2fs_xflags_map[i].xflag;
3117
3118 return xflags;
3119}
3120
3121/* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */
3122static inline u32 f2fs_xflags_to_iflags(u32 xflags)
3123{
3124 u32 iflags = 0;
3125 int i;
3126
3127 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++)
3128 if (xflags & f2fs_xflags_map[i].xflag)
3129 iflags |= f2fs_xflags_map[i].iflag;
3130
3131 return iflags;
3132}
3133
3134static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
3135{
3136 struct f2fs_inode_info *fi = F2FS_I(inode);
3137
3138 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags));
3139
3140 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3141 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3142}
3143
3144static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
3145{
3146 struct inode *inode = file_inode(filp);
3147 struct fsxattr fa;
3148
3149 f2fs_fill_fsxattr(inode, &fa);
3150
3151 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
3152 return -EFAULT;
3153 return 0;
3154}
3155
3156static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
3157{
3158 struct inode *inode = file_inode(filp);
3159 struct fsxattr fa, old_fa;
3160 u32 iflags;
3161 int err;
3162
3163 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
3164 return -EFAULT;
3165
3166 /* Make sure caller has proper permission */
3167 if (!inode_owner_or_capable(inode))
3168 return -EACCES;
3169
3170 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS)
3171 return -EOPNOTSUPP;
3172
3173 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags);
3174 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3175 return -EOPNOTSUPP;
3176
3177 err = mnt_want_write_file(filp);
3178 if (err)
3179 return err;
3180
3181 inode_lock(inode);
3182
3183 f2fs_fill_fsxattr(inode, &old_fa);
3184 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
3185 if (err)
3186 goto out;
3187
3188 err = f2fs_setflags_common(inode, iflags,
3189 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS));
3190 if (err)
3191 goto out;
3192
3193 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
3194out:
3195 inode_unlock(inode);
3196 mnt_drop_write_file(filp);
3197 return err;
3198}
3199
3200int f2fs_pin_file_control(struct inode *inode, bool inc)
3201{
3202 struct f2fs_inode_info *fi = F2FS_I(inode);
3203 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3204
3205 /* Use i_gc_failures for normal file as a risk signal. */
3206 if (inc)
3207 f2fs_i_gc_failures_write(inode,
3208 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3209
3210 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3211 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3212 __func__, inode->i_ino,
3213 fi->i_gc_failures[GC_FAILURE_PIN]);
3214 clear_inode_flag(inode, FI_PIN_FILE);
3215 return -EAGAIN;
3216 }
3217 return 0;
3218}
3219
3220static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3221{
3222 struct inode *inode = file_inode(filp);
3223 __u32 pin;
3224 int ret = 0;
3225
3226 if (get_user(pin, (__u32 __user *)arg))
3227 return -EFAULT;
3228
3229 if (!S_ISREG(inode->i_mode))
3230 return -EINVAL;
3231
3232 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3233 return -EROFS;
3234
3235 ret = mnt_want_write_file(filp);
3236 if (ret)
3237 return ret;
3238
3239 inode_lock(inode);
3240
3241 if (f2fs_should_update_outplace(inode, NULL)) {
3242 ret = -EINVAL;
3243 goto out;
3244 }
3245
3246 if (!pin) {
3247 clear_inode_flag(inode, FI_PIN_FILE);
3248 f2fs_i_gc_failures_write(inode, 0);
3249 goto done;
3250 }
3251
3252 if (f2fs_pin_file_control(inode, false)) {
3253 ret = -EAGAIN;
3254 goto out;
3255 }
3256
3257 ret = f2fs_convert_inline_inode(inode);
3258 if (ret)
3259 goto out;
3260
3261 if (f2fs_disable_compressed_file(inode)) {
3262 ret = -EOPNOTSUPP;
3263 goto out;
3264 }
3265
3266 set_inode_flag(inode, FI_PIN_FILE);
3267 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3268done:
3269 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3270out:
3271 inode_unlock(inode);
3272 mnt_drop_write_file(filp);
3273 return ret;
3274}
3275
3276static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3277{
3278 struct inode *inode = file_inode(filp);
3279 __u32 pin = 0;
3280
3281 if (is_inode_flag_set(inode, FI_PIN_FILE))
3282 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3283 return put_user(pin, (u32 __user *)arg);
3284}
3285
3286int f2fs_precache_extents(struct inode *inode)
3287{
3288 struct f2fs_inode_info *fi = F2FS_I(inode);
3289 struct f2fs_map_blocks map;
3290 pgoff_t m_next_extent;
3291 loff_t end;
3292 int err;
3293
3294 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3295 return -EOPNOTSUPP;
3296
3297 map.m_lblk = 0;
3298 map.m_next_pgofs = NULL;
3299 map.m_next_extent = &m_next_extent;
3300 map.m_seg_type = NO_CHECK_TYPE;
3301 map.m_may_create = false;
3302 end = F2FS_I_SB(inode)->max_file_blocks;
3303
3304 while (map.m_lblk < end) {
3305 map.m_len = end - map.m_lblk;
3306
3307 down_write(&fi->i_gc_rwsem[WRITE]);
3308 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
3309 up_write(&fi->i_gc_rwsem[WRITE]);
3310 if (err)
3311 return err;
3312
3313 map.m_lblk = m_next_extent;
3314 }
3315
3316 return err;
3317}
3318
3319static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
3320{
3321 return f2fs_precache_extents(file_inode(filp));
3322}
3323
3324static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3325{
3326 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3327 __u64 block_count;
3328
3329 if (!capable(CAP_SYS_ADMIN))
3330 return -EPERM;
3331
3332 if (f2fs_readonly(sbi->sb))
3333 return -EROFS;
3334
3335 if (copy_from_user(&block_count, (void __user *)arg,
3336 sizeof(block_count)))
3337 return -EFAULT;
3338
3339 return f2fs_resize_fs(sbi, block_count);
3340}
3341
3342static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3343{
3344 struct inode *inode = file_inode(filp);
3345
3346 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3347
3348 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3349 f2fs_warn(F2FS_I_SB(inode),
3350 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n",
3351 inode->i_ino);
3352 return -EOPNOTSUPP;
3353 }
3354
3355 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3356}
3357
3358static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3359{
3360 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3361 return -EOPNOTSUPP;
3362
3363 return fsverity_ioctl_measure(filp, (void __user *)arg);
3364}
3365
3366static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3367{
3368 struct inode *inode = file_inode(filp);
3369 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3370 char *vbuf;
3371 int count;
3372 int err = 0;
3373
3374 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3375 if (!vbuf)
3376 return -ENOMEM;
3377
3378 down_read(&sbi->sb_lock);
3379 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3380 ARRAY_SIZE(sbi->raw_super->volume_name),
3381 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3382 up_read(&sbi->sb_lock);
3383
3384 if (copy_to_user((char __user *)arg, vbuf,
3385 min(FSLABEL_MAX, count)))
3386 err = -EFAULT;
3387
3388 kvfree(vbuf);
3389 return err;
3390}
3391
3392static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3393{
3394 struct inode *inode = file_inode(filp);
3395 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3396 char *vbuf;
3397 int err = 0;
3398
3399 if (!capable(CAP_SYS_ADMIN))
3400 return -EPERM;
3401
3402 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3403 if (IS_ERR(vbuf))
3404 return PTR_ERR(vbuf);
3405
3406 err = mnt_want_write_file(filp);
3407 if (err)
3408 goto out;
3409
3410 down_write(&sbi->sb_lock);
3411
3412 memset(sbi->raw_super->volume_name, 0,
3413 sizeof(sbi->raw_super->volume_name));
3414 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3415 sbi->raw_super->volume_name,
3416 ARRAY_SIZE(sbi->raw_super->volume_name));
3417
3418 err = f2fs_commit_super(sbi, false);
3419
3420 up_write(&sbi->sb_lock);
3421
3422 mnt_drop_write_file(filp);
3423out:
3424 kfree(vbuf);
3425 return err;
3426}
3427
3428static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
3429{
3430 struct inode *inode = file_inode(filp);
3431 __u64 blocks;
3432
3433 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3434 return -EOPNOTSUPP;
3435
3436 if (!f2fs_compressed_file(inode))
3437 return -EINVAL;
3438
3439 blocks = F2FS_I(inode)->i_compr_blocks;
3440 return put_user(blocks, (u64 __user *)arg);
3441}
3442
3443static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3444{
3445 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3446 unsigned int released_blocks = 0;
3447 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3448 block_t blkaddr;
3449 int i;
3450
3451 for (i = 0; i < count; i++) {
3452 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3453 dn->ofs_in_node + i);
3454
3455 if (!__is_valid_data_blkaddr(blkaddr))
3456 continue;
3457 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3458 DATA_GENERIC_ENHANCE)))
3459 return -EFSCORRUPTED;
3460 }
3461
3462 while (count) {
3463 int compr_blocks = 0;
3464
3465 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3466 blkaddr = f2fs_data_blkaddr(dn);
3467
3468 if (i == 0) {
3469 if (blkaddr == COMPRESS_ADDR)
3470 continue;
3471 dn->ofs_in_node += cluster_size;
3472 goto next;
3473 }
3474
3475 if (__is_valid_data_blkaddr(blkaddr))
3476 compr_blocks++;
3477
3478 if (blkaddr != NEW_ADDR)
3479 continue;
3480
3481 dn->data_blkaddr = NULL_ADDR;
3482 f2fs_set_data_blkaddr(dn);
3483 }
3484
3485 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3486 dec_valid_block_count(sbi, dn->inode,
3487 cluster_size - compr_blocks);
3488
3489 released_blocks += cluster_size - compr_blocks;
3490next:
3491 count -= cluster_size;
3492 }
3493
3494 return released_blocks;
3495}
3496
3497static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3498{
3499 struct inode *inode = file_inode(filp);
3500 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3501 pgoff_t page_idx = 0, last_idx;
3502 unsigned int released_blocks = 0;
3503 int ret;
3504 int writecount;
3505
3506 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3507 return -EOPNOTSUPP;
3508
3509 if (!f2fs_compressed_file(inode))
3510 return -EINVAL;
3511
3512 if (f2fs_readonly(sbi->sb))
3513 return -EROFS;
3514
3515 ret = mnt_want_write_file(filp);
3516 if (ret)
3517 return ret;
3518
3519 f2fs_balance_fs(F2FS_I_SB(inode), true);
3520
3521 inode_lock(inode);
3522
3523 writecount = atomic_read(&inode->i_writecount);
3524 if ((filp->f_mode & FMODE_WRITE && writecount != 1) || writecount) {
3525 ret = -EBUSY;
3526 goto out;
3527 }
3528
3529 if (IS_IMMUTABLE(inode)) {
3530 ret = -EINVAL;
3531 goto out;
3532 }
3533
3534 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3535 if (ret)
3536 goto out;
3537
3538 F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
3539 f2fs_set_inode_flags(inode);
3540 inode->i_ctime = current_time(inode);
3541 f2fs_mark_inode_dirty_sync(inode, true);
3542
3543 if (!F2FS_I(inode)->i_compr_blocks)
3544 goto out;
3545
3546 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3547 down_write(&F2FS_I(inode)->i_mmap_sem);
3548
3549 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3550
3551 while (page_idx < last_idx) {
3552 struct dnode_of_data dn;
3553 pgoff_t end_offset, count;
3554
3555 set_new_dnode(&dn, inode, NULL, NULL, 0);
3556 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3557 if (ret) {
3558 if (ret == -ENOENT) {
3559 page_idx = f2fs_get_next_page_offset(&dn,
3560 page_idx);
3561 ret = 0;
3562 continue;
3563 }
3564 break;
3565 }
3566
3567 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3568 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3569 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3570
3571 ret = release_compress_blocks(&dn, count);
3572
3573 f2fs_put_dnode(&dn);
3574
3575 if (ret < 0)
3576 break;
3577
3578 page_idx += count;
3579 released_blocks += ret;
3580 }
3581
3582 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3583 up_write(&F2FS_I(inode)->i_mmap_sem);
3584out:
3585 inode_unlock(inode);
3586
3587 mnt_drop_write_file(filp);
3588
3589 if (ret >= 0) {
3590 ret = put_user(released_blocks, (u64 __user *)arg);
3591 } else if (released_blocks && F2FS_I(inode)->i_compr_blocks) {
3592 set_sbi_flag(sbi, SBI_NEED_FSCK);
3593 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3594 "iblocks=%llu, released=%u, compr_blocks=%llu, "
3595 "run fsck to fix.",
3596 __func__, inode->i_ino, inode->i_blocks,
3597 released_blocks,
3598 F2FS_I(inode)->i_compr_blocks);
3599 }
3600
3601 return ret;
3602}
3603
3604static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3605{
3606 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3607 unsigned int reserved_blocks = 0;
3608 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3609 block_t blkaddr;
3610 int i;
3611
3612 for (i = 0; i < count; i++) {
3613 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3614 dn->ofs_in_node + i);
3615
3616 if (!__is_valid_data_blkaddr(blkaddr))
3617 continue;
3618 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3619 DATA_GENERIC_ENHANCE)))
3620 return -EFSCORRUPTED;
3621 }
3622
3623 while (count) {
3624 int compr_blocks = 0;
3625 blkcnt_t reserved;
3626 int ret;
3627
3628 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3629 blkaddr = f2fs_data_blkaddr(dn);
3630
3631 if (i == 0) {
3632 if (blkaddr == COMPRESS_ADDR)
3633 continue;
3634 dn->ofs_in_node += cluster_size;
3635 goto next;
3636 }
3637
3638 if (__is_valid_data_blkaddr(blkaddr)) {
3639 compr_blocks++;
3640 continue;
3641 }
3642
3643 dn->data_blkaddr = NEW_ADDR;
3644 f2fs_set_data_blkaddr(dn);
3645 }
3646
3647 reserved = cluster_size - compr_blocks;
3648 ret = inc_valid_block_count(sbi, dn->inode, &reserved);
3649 if (ret)
3650 return ret;
3651
3652 if (reserved != cluster_size - compr_blocks)
3653 return -ENOSPC;
3654
3655 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3656
3657 reserved_blocks += reserved;
3658next:
3659 count -= cluster_size;
3660 }
3661
3662 return reserved_blocks;
3663}
3664
3665static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3666{
3667 struct inode *inode = file_inode(filp);
3668 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3669 pgoff_t page_idx = 0, last_idx;
3670 unsigned int reserved_blocks = 0;
3671 int ret;
3672
3673 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3674 return -EOPNOTSUPP;
3675
3676 if (!f2fs_compressed_file(inode))
3677 return -EINVAL;
3678
3679 if (f2fs_readonly(sbi->sb))
3680 return -EROFS;
3681
3682 ret = mnt_want_write_file(filp);
3683 if (ret)
3684 return ret;
3685
3686 if (F2FS_I(inode)->i_compr_blocks)
3687 goto out;
3688
3689 f2fs_balance_fs(F2FS_I_SB(inode), true);
3690
3691 inode_lock(inode);
3692
3693 if (!IS_IMMUTABLE(inode)) {
3694 ret = -EINVAL;
3695 goto unlock_inode;
3696 }
3697
3698 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3699 down_write(&F2FS_I(inode)->i_mmap_sem);
3700
3701 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3702
3703 while (page_idx < last_idx) {
3704 struct dnode_of_data dn;
3705 pgoff_t end_offset, count;
3706
3707 set_new_dnode(&dn, inode, NULL, NULL, 0);
3708 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3709 if (ret) {
3710 if (ret == -ENOENT) {
3711 page_idx = f2fs_get_next_page_offset(&dn,
3712 page_idx);
3713 ret = 0;
3714 continue;
3715 }
3716 break;
3717 }
3718
3719 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3720 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3721 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3722
3723 ret = reserve_compress_blocks(&dn, count);
3724
3725 f2fs_put_dnode(&dn);
3726
3727 if (ret < 0)
3728 break;
3729
3730 page_idx += count;
3731 reserved_blocks += ret;
3732 }
3733
3734 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3735 up_write(&F2FS_I(inode)->i_mmap_sem);
3736
3737 if (ret >= 0) {
3738 F2FS_I(inode)->i_flags &= ~F2FS_IMMUTABLE_FL;
3739 f2fs_set_inode_flags(inode);
3740 inode->i_ctime = current_time(inode);
3741 f2fs_mark_inode_dirty_sync(inode, true);
3742 }
3743unlock_inode:
3744 inode_unlock(inode);
3745out:
3746 mnt_drop_write_file(filp);
3747
3748 if (ret >= 0) {
3749 ret = put_user(reserved_blocks, (u64 __user *)arg);
3750 } else if (reserved_blocks && F2FS_I(inode)->i_compr_blocks) {
3751 set_sbi_flag(sbi, SBI_NEED_FSCK);
3752 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3753 "iblocks=%llu, reserved=%u, compr_blocks=%llu, "
3754 "run fsck to fix.",
3755 __func__, inode->i_ino, inode->i_blocks,
3756 reserved_blocks,
3757 F2FS_I(inode)->i_compr_blocks);
3758 }
3759
3760 return ret;
3761}
3762
3763static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3764 pgoff_t off, block_t block, block_t len, u32 flags)
3765{
3766 struct request_queue *q = bdev_get_queue(bdev);
3767 sector_t sector = SECTOR_FROM_BLOCK(block);
3768 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3769 int ret = 0;
3770
3771 if (!q)
3772 return -ENXIO;
3773
3774 if (flags & F2FS_TRIM_FILE_DISCARD)
3775 ret = blkdev_issue_discard(bdev, sector, nr_sects, GFP_NOFS,
3776 blk_queue_secure_erase(q) ?
3777 BLKDEV_DISCARD_SECURE : 0);
3778
3779 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3780 if (IS_ENCRYPTED(inode))
3781 ret = fscrypt_zeroout_range(inode, off, block, len);
3782 else
3783 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3784 GFP_NOFS, 0);
3785 }
3786
3787 return ret;
3788}
3789
3790static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3791{
3792 struct inode *inode = file_inode(filp);
3793 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3794 struct address_space *mapping = inode->i_mapping;
3795 struct block_device *prev_bdev = NULL;
3796 struct f2fs_sectrim_range range;
3797 pgoff_t index, pg_end, prev_index = 0;
3798 block_t prev_block = 0, len = 0;
3799 loff_t end_addr;
3800 bool to_end = false;
3801 int ret = 0;
3802
3803 if (!(filp->f_mode & FMODE_WRITE))
3804 return -EBADF;
3805
3806 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
3807 sizeof(range)))
3808 return -EFAULT;
3809
3810 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
3811 !S_ISREG(inode->i_mode))
3812 return -EINVAL;
3813
3814 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
3815 !f2fs_hw_support_discard(sbi)) ||
3816 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
3817 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
3818 return -EOPNOTSUPP;
3819
3820 file_start_write(filp);
3821 inode_lock(inode);
3822
3823 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
3824 range.start >= inode->i_size) {
3825 ret = -EINVAL;
3826 goto err;
3827 }
3828
3829 if (range.len == 0)
3830 goto err;
3831
3832 if (inode->i_size - range.start > range.len) {
3833 end_addr = range.start + range.len;
3834 } else {
3835 end_addr = range.len == (u64)-1 ?
3836 sbi->sb->s_maxbytes : inode->i_size;
3837 to_end = true;
3838 }
3839
3840 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
3841 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
3842 ret = -EINVAL;
3843 goto err;
3844 }
3845
3846 index = F2FS_BYTES_TO_BLK(range.start);
3847 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
3848
3849 ret = f2fs_convert_inline_inode(inode);
3850 if (ret)
3851 goto err;
3852
3853 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3854 down_write(&F2FS_I(inode)->i_mmap_sem);
3855
3856 ret = filemap_write_and_wait_range(mapping, range.start,
3857 to_end ? LLONG_MAX : end_addr - 1);
3858 if (ret)
3859 goto out;
3860
3861 truncate_inode_pages_range(mapping, range.start,
3862 to_end ? -1 : end_addr - 1);
3863
3864 while (index < pg_end) {
3865 struct dnode_of_data dn;
3866 pgoff_t end_offset, count;
3867 int i;
3868
3869 set_new_dnode(&dn, inode, NULL, NULL, 0);
3870 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3871 if (ret) {
3872 if (ret == -ENOENT) {
3873 index = f2fs_get_next_page_offset(&dn, index);
3874 continue;
3875 }
3876 goto out;
3877 }
3878
3879 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3880 count = min(end_offset - dn.ofs_in_node, pg_end - index);
3881 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
3882 struct block_device *cur_bdev;
3883 block_t blkaddr = f2fs_data_blkaddr(&dn);
3884
3885 if (!__is_valid_data_blkaddr(blkaddr))
3886 continue;
3887
3888 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3889 DATA_GENERIC_ENHANCE)) {
3890 ret = -EFSCORRUPTED;
3891 f2fs_put_dnode(&dn);
3892 goto out;
3893 }
3894
3895 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
3896 if (f2fs_is_multi_device(sbi)) {
3897 int di = f2fs_target_device_index(sbi, blkaddr);
3898
3899 blkaddr -= FDEV(di).start_blk;
3900 }
3901
3902 if (len) {
3903 if (prev_bdev == cur_bdev &&
3904 index == prev_index + len &&
3905 blkaddr == prev_block + len) {
3906 len++;
3907 } else {
3908 ret = f2fs_secure_erase(prev_bdev,
3909 inode, prev_index, prev_block,
3910 len, range.flags);
3911 if (ret) {
3912 f2fs_put_dnode(&dn);
3913 goto out;
3914 }
3915
3916 len = 0;
3917 }
3918 }
3919
3920 if (!len) {
3921 prev_bdev = cur_bdev;
3922 prev_index = index;
3923 prev_block = blkaddr;
3924 len = 1;
3925 }
3926 }
3927
3928 f2fs_put_dnode(&dn);
3929
3930 if (fatal_signal_pending(current)) {
3931 ret = -EINTR;
3932 goto out;
3933 }
3934 cond_resched();
3935 }
3936
3937 if (len)
3938 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
3939 prev_block, len, range.flags);
3940out:
3941 up_write(&F2FS_I(inode)->i_mmap_sem);
3942 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3943err:
3944 inode_unlock(inode);
3945 file_end_write(filp);
3946
3947 return ret;
3948}
3949
3950long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3951{
3952 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
3953 return -EIO;
3954 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
3955 return -ENOSPC;
3956
3957 switch (cmd) {
3958 case FS_IOC_GETFLAGS:
3959 return f2fs_ioc_getflags(filp, arg);
3960 case FS_IOC_SETFLAGS:
3961 return f2fs_ioc_setflags(filp, arg);
3962 case FS_IOC_GETVERSION:
3963 return f2fs_ioc_getversion(filp, arg);
3964 case F2FS_IOC_START_ATOMIC_WRITE:
3965 return f2fs_ioc_start_atomic_write(filp);
3966 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
3967 return f2fs_ioc_commit_atomic_write(filp);
3968 case F2FS_IOC_START_VOLATILE_WRITE:
3969 return f2fs_ioc_start_volatile_write(filp);
3970 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
3971 return f2fs_ioc_release_volatile_write(filp);
3972 case F2FS_IOC_ABORT_VOLATILE_WRITE:
3973 return f2fs_ioc_abort_volatile_write(filp);
3974 case F2FS_IOC_SHUTDOWN:
3975 return f2fs_ioc_shutdown(filp, arg);
3976 case FITRIM:
3977 return f2fs_ioc_fitrim(filp, arg);
3978 case FS_IOC_SET_ENCRYPTION_POLICY:
3979 return f2fs_ioc_set_encryption_policy(filp, arg);
3980 case FS_IOC_GET_ENCRYPTION_POLICY:
3981 return f2fs_ioc_get_encryption_policy(filp, arg);
3982 case FS_IOC_GET_ENCRYPTION_PWSALT:
3983 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
3984 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
3985 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
3986 case FS_IOC_ADD_ENCRYPTION_KEY:
3987 return f2fs_ioc_add_encryption_key(filp, arg);
3988 case FS_IOC_REMOVE_ENCRYPTION_KEY:
3989 return f2fs_ioc_remove_encryption_key(filp, arg);
3990 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3991 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
3992 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
3993 return f2fs_ioc_get_encryption_key_status(filp, arg);
3994 case FS_IOC_GET_ENCRYPTION_NONCE:
3995 return f2fs_ioc_get_encryption_nonce(filp, arg);
3996 case F2FS_IOC_GARBAGE_COLLECT:
3997 return f2fs_ioc_gc(filp, arg);
3998 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
3999 return f2fs_ioc_gc_range(filp, arg);
4000 case F2FS_IOC_WRITE_CHECKPOINT:
4001 return f2fs_ioc_write_checkpoint(filp, arg);
4002 case F2FS_IOC_DEFRAGMENT:
4003 return f2fs_ioc_defragment(filp, arg);
4004 case F2FS_IOC_MOVE_RANGE:
4005 return f2fs_ioc_move_range(filp, arg);
4006 case F2FS_IOC_FLUSH_DEVICE:
4007 return f2fs_ioc_flush_device(filp, arg);
4008 case F2FS_IOC_GET_FEATURES:
4009 return f2fs_ioc_get_features(filp, arg);
4010 case FS_IOC_FSGETXATTR:
4011 return f2fs_ioc_fsgetxattr(filp, arg);
4012 case FS_IOC_FSSETXATTR:
4013 return f2fs_ioc_fssetxattr(filp, arg);
4014 case F2FS_IOC_GET_PIN_FILE:
4015 return f2fs_ioc_get_pin_file(filp, arg);
4016 case F2FS_IOC_SET_PIN_FILE:
4017 return f2fs_ioc_set_pin_file(filp, arg);
4018 case F2FS_IOC_PRECACHE_EXTENTS:
4019 return f2fs_ioc_precache_extents(filp, arg);
4020 case F2FS_IOC_RESIZE_FS:
4021 return f2fs_ioc_resize_fs(filp, arg);
4022 case FS_IOC_ENABLE_VERITY:
4023 return f2fs_ioc_enable_verity(filp, arg);
4024 case FS_IOC_MEASURE_VERITY:
4025 return f2fs_ioc_measure_verity(filp, arg);
4026 case FS_IOC_GETFSLABEL:
4027 return f2fs_ioc_getfslabel(filp, arg);
4028 case FS_IOC_SETFSLABEL:
4029 return f2fs_ioc_setfslabel(filp, arg);
4030 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4031 return f2fs_get_compress_blocks(filp, arg);
4032 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4033 return f2fs_release_compress_blocks(filp, arg);
4034 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4035 return f2fs_reserve_compress_blocks(filp, arg);
4036 case F2FS_IOC_SEC_TRIM_FILE:
4037 return f2fs_sec_trim_file(filp, arg);
4038 default:
4039 return -ENOTTY;
4040 }
4041}
4042
4043static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
4044{
4045 struct file *file = iocb->ki_filp;
4046 struct inode *inode = file_inode(file);
4047 int ret;
4048
4049 if (!f2fs_is_compress_backend_ready(inode))
4050 return -EOPNOTSUPP;
4051
4052 ret = generic_file_read_iter(iocb, iter);
4053
4054 if (ret > 0)
4055 f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret);
4056
4057 return ret;
4058}
4059
4060static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4061{
4062 struct file *file = iocb->ki_filp;
4063 struct inode *inode = file_inode(file);
4064 ssize_t ret;
4065
4066 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4067 ret = -EIO;
4068 goto out;
4069 }
4070
4071 if (!f2fs_is_compress_backend_ready(inode)) {
4072 ret = -EOPNOTSUPP;
4073 goto out;
4074 }
4075
4076 if (iocb->ki_flags & IOCB_NOWAIT) {
4077 if (!inode_trylock(inode)) {
4078 ret = -EAGAIN;
4079 goto out;
4080 }
4081 } else {
4082 inode_lock(inode);
4083 }
4084
4085 ret = generic_write_checks(iocb, from);
4086 if (ret > 0) {
4087 bool preallocated = false;
4088 size_t target_size = 0;
4089 int err;
4090
4091 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
4092 set_inode_flag(inode, FI_NO_PREALLOC);
4093
4094 if ((iocb->ki_flags & IOCB_NOWAIT)) {
4095 if (!f2fs_overwrite_io(inode, iocb->ki_pos,
4096 iov_iter_count(from)) ||
4097 f2fs_has_inline_data(inode) ||
4098 f2fs_force_buffered_io(inode, iocb, from)) {
4099 clear_inode_flag(inode, FI_NO_PREALLOC);
4100 inode_unlock(inode);
4101 ret = -EAGAIN;
4102 goto out;
4103 }
4104 goto write;
4105 }
4106
4107 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
4108 goto write;
4109
4110 if (iocb->ki_flags & IOCB_DIRECT) {
4111 /*
4112 * Convert inline data for Direct I/O before entering
4113 * f2fs_direct_IO().
4114 */
4115 err = f2fs_convert_inline_inode(inode);
4116 if (err)
4117 goto out_err;
4118 /*
4119 * If force_buffere_io() is true, we have to allocate
4120 * blocks all the time, since f2fs_direct_IO will fall
4121 * back to buffered IO.
4122 */
4123 if (!f2fs_force_buffered_io(inode, iocb, from) &&
4124 allow_outplace_dio(inode, iocb, from))
4125 goto write;
4126 }
4127 preallocated = true;
4128 target_size = iocb->ki_pos + iov_iter_count(from);
4129
4130 err = f2fs_preallocate_blocks(iocb, from);
4131 if (err) {
4132out_err:
4133 clear_inode_flag(inode, FI_NO_PREALLOC);
4134 inode_unlock(inode);
4135 ret = err;
4136 goto out;
4137 }
4138write:
4139 ret = __generic_file_write_iter(iocb, from);
4140 clear_inode_flag(inode, FI_NO_PREALLOC);
4141
4142 /* if we couldn't write data, we should deallocate blocks. */
4143 if (preallocated && i_size_read(inode) < target_size)
4144 f2fs_truncate(inode);
4145
4146 if (ret > 0)
4147 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
4148 }
4149 inode_unlock(inode);
4150out:
4151 trace_f2fs_file_write_iter(inode, iocb->ki_pos,
4152 iov_iter_count(from), ret);
4153 if (ret > 0)
4154 ret = generic_write_sync(iocb, ret);
4155 return ret;
4156}
4157
4158#ifdef CONFIG_COMPAT
4159long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4160{
4161 switch (cmd) {
4162 case FS_IOC32_GETFLAGS:
4163 cmd = FS_IOC_GETFLAGS;
4164 break;
4165 case FS_IOC32_SETFLAGS:
4166 cmd = FS_IOC_SETFLAGS;
4167 break;
4168 case FS_IOC32_GETVERSION:
4169 cmd = FS_IOC_GETVERSION;
4170 break;
4171 case F2FS_IOC_START_ATOMIC_WRITE:
4172 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4173 case F2FS_IOC_START_VOLATILE_WRITE:
4174 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4175 case F2FS_IOC_ABORT_VOLATILE_WRITE:
4176 case F2FS_IOC_SHUTDOWN:
4177 case FITRIM:
4178 case FS_IOC_SET_ENCRYPTION_POLICY:
4179 case FS_IOC_GET_ENCRYPTION_PWSALT:
4180 case FS_IOC_GET_ENCRYPTION_POLICY:
4181 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4182 case FS_IOC_ADD_ENCRYPTION_KEY:
4183 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4184 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4185 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4186 case FS_IOC_GET_ENCRYPTION_NONCE:
4187 case F2FS_IOC_GARBAGE_COLLECT:
4188 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4189 case F2FS_IOC_WRITE_CHECKPOINT:
4190 case F2FS_IOC_DEFRAGMENT:
4191 case F2FS_IOC_MOVE_RANGE:
4192 case F2FS_IOC_FLUSH_DEVICE:
4193 case F2FS_IOC_GET_FEATURES:
4194 case FS_IOC_FSGETXATTR:
4195 case FS_IOC_FSSETXATTR:
4196 case F2FS_IOC_GET_PIN_FILE:
4197 case F2FS_IOC_SET_PIN_FILE:
4198 case F2FS_IOC_PRECACHE_EXTENTS:
4199 case F2FS_IOC_RESIZE_FS:
4200 case FS_IOC_ENABLE_VERITY:
4201 case FS_IOC_MEASURE_VERITY:
4202 case FS_IOC_GETFSLABEL:
4203 case FS_IOC_SETFSLABEL:
4204 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4205 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4206 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4207 case F2FS_IOC_SEC_TRIM_FILE:
4208 break;
4209 default:
4210 return -ENOIOCTLCMD;
4211 }
4212 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4213}
4214#endif
4215
4216const struct file_operations f2fs_file_operations = {
4217 .llseek = f2fs_llseek,
4218 .read_iter = f2fs_file_read_iter,
4219 .write_iter = f2fs_file_write_iter,
4220 .open = f2fs_file_open,
4221 .release = f2fs_release_file,
4222 .mmap = f2fs_file_mmap,
4223 .flush = f2fs_file_flush,
4224 .fsync = f2fs_sync_file,
4225 .fallocate = f2fs_fallocate,
4226 .unlocked_ioctl = f2fs_ioctl,
4227#ifdef CONFIG_COMPAT
4228 .compat_ioctl = f2fs_compat_ioctl,
4229#endif
4230 .splice_read = generic_file_splice_read,
4231 .splice_write = iter_file_splice_write,
4232};