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