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