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