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