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