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