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 25 #include "f2fs.h" 26 #include "node.h" 27 #include "segment.h" 28 #include "xattr.h" 29 #include "acl.h" 30 #include "gc.h" 31 #include "trace.h" 32 #include <trace/events/f2fs.h> 33 34 static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf) 35 { 36 struct inode *inode = file_inode(vmf->vma->vm_file); 37 vm_fault_t ret; 38 39 down_read(&F2FS_I(inode)->i_mmap_sem); 40 ret = filemap_fault(vmf); 41 up_read(&F2FS_I(inode)->i_mmap_sem); 42 43 trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret); 44 45 return ret; 46 } 47 48 static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) 49 { 50 struct page *page = vmf->page; 51 struct inode *inode = file_inode(vmf->vma->vm_file); 52 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 53 struct dnode_of_data dn; 54 bool need_alloc = true; 55 int err = 0; 56 57 if (unlikely(f2fs_cp_error(sbi))) { 58 err = -EIO; 59 goto err; 60 } 61 62 if (!f2fs_is_checkpoint_ready(sbi)) { 63 err = -ENOSPC; 64 goto err; 65 } 66 67 #ifdef CONFIG_F2FS_FS_COMPRESSION 68 if (f2fs_compressed_file(inode)) { 69 int ret = f2fs_is_compressed_cluster(inode, page->index); 70 71 if (ret < 0) { 72 err = ret; 73 goto err; 74 } else if (ret) { 75 if (ret < F2FS_I(inode)->i_cluster_size) { 76 err = -EAGAIN; 77 goto err; 78 } 79 need_alloc = false; 80 } 81 } 82 #endif 83 /* should do out of any locked page */ 84 if (need_alloc) 85 f2fs_balance_fs(sbi, true); 86 87 sb_start_pagefault(inode->i_sb); 88 89 f2fs_bug_on(sbi, f2fs_has_inline_data(inode)); 90 91 file_update_time(vmf->vma->vm_file); 92 down_read(&F2FS_I(inode)->i_mmap_sem); 93 lock_page(page); 94 if (unlikely(page->mapping != inode->i_mapping || 95 page_offset(page) > i_size_read(inode) || 96 !PageUptodate(page))) { 97 unlock_page(page); 98 err = -EFAULT; 99 goto out_sem; 100 } 101 102 if (need_alloc) { 103 /* block allocation */ 104 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true); 105 set_new_dnode(&dn, inode, NULL, NULL, 0); 106 err = f2fs_get_block(&dn, page->index); 107 f2fs_put_dnode(&dn); 108 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false); 109 if (err) { 110 unlock_page(page); 111 goto out_sem; 112 } 113 } 114 115 /* fill the page */ 116 f2fs_wait_on_page_writeback(page, DATA, false, true); 117 118 /* wait for GCed page writeback via META_MAPPING */ 119 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr); 120 121 /* 122 * check to see if the page is mapped already (no holes) 123 */ 124 if (PageMappedToDisk(page)) 125 goto out_sem; 126 127 /* page is wholly or partially inside EOF */ 128 if (((loff_t)(page->index + 1) << PAGE_SHIFT) > 129 i_size_read(inode)) { 130 loff_t offset; 131 132 offset = i_size_read(inode) & ~PAGE_MASK; 133 zero_user_segment(page, offset, PAGE_SIZE); 134 } 135 set_page_dirty(page); 136 if (!PageUptodate(page)) 137 SetPageUptodate(page); 138 139 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE); 140 f2fs_update_time(sbi, REQ_TIME); 141 142 trace_f2fs_vm_page_mkwrite(page, DATA); 143 out_sem: 144 up_read(&F2FS_I(inode)->i_mmap_sem); 145 146 sb_end_pagefault(inode->i_sb); 147 err: 148 return block_page_mkwrite_return(err); 149 } 150 151 static const struct vm_operations_struct f2fs_file_vm_ops = { 152 .fault = f2fs_filemap_fault, 153 .map_pages = filemap_map_pages, 154 .page_mkwrite = f2fs_vm_page_mkwrite, 155 }; 156 157 static int get_parent_ino(struct inode *inode, nid_t *pino) 158 { 159 struct dentry *dentry; 160 161 inode = igrab(inode); 162 dentry = d_find_any_alias(inode); 163 iput(inode); 164 if (!dentry) 165 return 0; 166 167 *pino = parent_ino(dentry); 168 dput(dentry); 169 return 1; 170 } 171 172 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode) 173 { 174 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 175 enum cp_reason_type cp_reason = CP_NO_NEEDED; 176 177 if (!S_ISREG(inode->i_mode)) 178 cp_reason = CP_NON_REGULAR; 179 else if (f2fs_compressed_file(inode)) 180 cp_reason = CP_COMPRESSED; 181 else if (inode->i_nlink != 1) 182 cp_reason = CP_HARDLINK; 183 else if (is_sbi_flag_set(sbi, SBI_NEED_CP)) 184 cp_reason = CP_SB_NEED_CP; 185 else if (file_wrong_pino(inode)) 186 cp_reason = CP_WRONG_PINO; 187 else if (!f2fs_space_for_roll_forward(sbi)) 188 cp_reason = CP_NO_SPC_ROLL; 189 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino)) 190 cp_reason = CP_NODE_NEED_CP; 191 else if (test_opt(sbi, FASTBOOT)) 192 cp_reason = CP_FASTBOOT_MODE; 193 else if (F2FS_OPTION(sbi).active_logs == 2) 194 cp_reason = CP_SPEC_LOG_NUM; 195 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT && 196 f2fs_need_dentry_mark(sbi, inode->i_ino) && 197 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino, 198 TRANS_DIR_INO)) 199 cp_reason = CP_RECOVER_DIR; 200 201 return cp_reason; 202 } 203 204 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino) 205 { 206 struct page *i = find_get_page(NODE_MAPPING(sbi), ino); 207 bool ret = false; 208 /* But we need to avoid that there are some inode updates */ 209 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino)) 210 ret = true; 211 f2fs_put_page(i, 0); 212 return ret; 213 } 214 215 static void try_to_fix_pino(struct inode *inode) 216 { 217 struct f2fs_inode_info *fi = F2FS_I(inode); 218 nid_t pino; 219 220 down_write(&fi->i_sem); 221 if (file_wrong_pino(inode) && inode->i_nlink == 1 && 222 get_parent_ino(inode, &pino)) { 223 f2fs_i_pino_write(inode, pino); 224 file_got_pino(inode); 225 } 226 up_write(&fi->i_sem); 227 } 228 229 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end, 230 int datasync, bool atomic) 231 { 232 struct inode *inode = file->f_mapping->host; 233 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 234 nid_t ino = inode->i_ino; 235 int ret = 0; 236 enum cp_reason_type cp_reason = 0; 237 struct writeback_control wbc = { 238 .sync_mode = WB_SYNC_ALL, 239 .nr_to_write = LONG_MAX, 240 .for_reclaim = 0, 241 }; 242 unsigned int seq_id = 0; 243 244 if (unlikely(f2fs_readonly(inode->i_sb) || 245 is_sbi_flag_set(sbi, SBI_CP_DISABLED))) 246 return 0; 247 248 trace_f2fs_sync_file_enter(inode); 249 250 if (S_ISDIR(inode->i_mode)) 251 goto go_write; 252 253 /* if fdatasync is triggered, let's do in-place-update */ 254 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks) 255 set_inode_flag(inode, FI_NEED_IPU); 256 ret = file_write_and_wait_range(file, start, end); 257 clear_inode_flag(inode, FI_NEED_IPU); 258 259 if (ret) { 260 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret); 261 return ret; 262 } 263 264 /* if the inode is dirty, let's recover all the time */ 265 if (!f2fs_skip_inode_update(inode, datasync)) { 266 f2fs_write_inode(inode, NULL); 267 goto go_write; 268 } 269 270 /* 271 * if there is no written data, don't waste time to write recovery info. 272 */ 273 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) && 274 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) { 275 276 /* it may call write_inode just prior to fsync */ 277 if (need_inode_page_update(sbi, ino)) 278 goto go_write; 279 280 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) || 281 f2fs_exist_written_data(sbi, ino, UPDATE_INO)) 282 goto flush_out; 283 goto out; 284 } 285 go_write: 286 /* 287 * Both of fdatasync() and fsync() are able to be recovered from 288 * sudden-power-off. 289 */ 290 down_read(&F2FS_I(inode)->i_sem); 291 cp_reason = need_do_checkpoint(inode); 292 up_read(&F2FS_I(inode)->i_sem); 293 294 if (cp_reason) { 295 /* all the dirty node pages should be flushed for POR */ 296 ret = f2fs_sync_fs(inode->i_sb, 1); 297 298 /* 299 * We've secured consistency through sync_fs. Following pino 300 * will be used only for fsynced inodes after checkpoint. 301 */ 302 try_to_fix_pino(inode); 303 clear_inode_flag(inode, FI_APPEND_WRITE); 304 clear_inode_flag(inode, FI_UPDATE_WRITE); 305 goto out; 306 } 307 sync_nodes: 308 atomic_inc(&sbi->wb_sync_req[NODE]); 309 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id); 310 atomic_dec(&sbi->wb_sync_req[NODE]); 311 if (ret) 312 goto out; 313 314 /* if cp_error was enabled, we should avoid infinite loop */ 315 if (unlikely(f2fs_cp_error(sbi))) { 316 ret = -EIO; 317 goto out; 318 } 319 320 if (f2fs_need_inode_block_update(sbi, ino)) { 321 f2fs_mark_inode_dirty_sync(inode, true); 322 f2fs_write_inode(inode, NULL); 323 goto sync_nodes; 324 } 325 326 /* 327 * If it's atomic_write, it's just fine to keep write ordering. So 328 * here we don't need to wait for node write completion, since we use 329 * node chain which serializes node blocks. If one of node writes are 330 * reordered, we can see simply broken chain, resulting in stopping 331 * roll-forward recovery. It means we'll recover all or none node blocks 332 * given fsync mark. 333 */ 334 if (!atomic) { 335 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id); 336 if (ret) 337 goto out; 338 } 339 340 /* once recovery info is written, don't need to tack this */ 341 f2fs_remove_ino_entry(sbi, ino, APPEND_INO); 342 clear_inode_flag(inode, FI_APPEND_WRITE); 343 flush_out: 344 if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER) 345 ret = f2fs_issue_flush(sbi, inode->i_ino); 346 if (!ret) { 347 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO); 348 clear_inode_flag(inode, FI_UPDATE_WRITE); 349 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO); 350 } 351 f2fs_update_time(sbi, REQ_TIME); 352 out: 353 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret); 354 f2fs_trace_ios(NULL, 1); 355 return ret; 356 } 357 358 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) 359 { 360 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file))))) 361 return -EIO; 362 return f2fs_do_sync_file(file, start, end, datasync, false); 363 } 364 365 static pgoff_t __get_first_dirty_index(struct address_space *mapping, 366 pgoff_t pgofs, int whence) 367 { 368 struct page *page; 369 int nr_pages; 370 371 if (whence != SEEK_DATA) 372 return 0; 373 374 /* find first dirty page index */ 375 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY, 376 1, &page); 377 if (!nr_pages) 378 return ULONG_MAX; 379 pgofs = page->index; 380 put_page(page); 381 return pgofs; 382 } 383 384 static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr, 385 pgoff_t dirty, pgoff_t pgofs, int whence) 386 { 387 switch (whence) { 388 case SEEK_DATA: 389 if ((blkaddr == NEW_ADDR && dirty == pgofs) || 390 __is_valid_data_blkaddr(blkaddr)) 391 return true; 392 break; 393 case SEEK_HOLE: 394 if (blkaddr == NULL_ADDR) 395 return true; 396 break; 397 } 398 return false; 399 } 400 401 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) 402 { 403 struct inode *inode = file->f_mapping->host; 404 loff_t maxbytes = inode->i_sb->s_maxbytes; 405 struct dnode_of_data dn; 406 pgoff_t pgofs, end_offset, dirty; 407 loff_t data_ofs = offset; 408 loff_t isize; 409 int err = 0; 410 411 inode_lock(inode); 412 413 isize = i_size_read(inode); 414 if (offset >= isize) 415 goto fail; 416 417 /* handle inline data case */ 418 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) { 419 if (whence == SEEK_HOLE) 420 data_ofs = isize; 421 goto found; 422 } 423 424 pgofs = (pgoff_t)(offset >> PAGE_SHIFT); 425 426 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence); 427 428 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) { 429 set_new_dnode(&dn, inode, NULL, NULL, 0); 430 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE); 431 if (err && err != -ENOENT) { 432 goto fail; 433 } else if (err == -ENOENT) { 434 /* direct node does not exists */ 435 if (whence == SEEK_DATA) { 436 pgofs = f2fs_get_next_page_offset(&dn, pgofs); 437 continue; 438 } else { 439 goto found; 440 } 441 } 442 443 end_offset = ADDRS_PER_PAGE(dn.node_page, inode); 444 445 /* find data/hole in dnode block */ 446 for (; dn.ofs_in_node < end_offset; 447 dn.ofs_in_node++, pgofs++, 448 data_ofs = (loff_t)pgofs << PAGE_SHIFT) { 449 block_t blkaddr; 450 451 blkaddr = datablock_addr(dn.inode, 452 dn.node_page, dn.ofs_in_node); 453 454 if (__is_valid_data_blkaddr(blkaddr) && 455 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode), 456 blkaddr, DATA_GENERIC_ENHANCE)) { 457 f2fs_put_dnode(&dn); 458 goto fail; 459 } 460 461 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty, 462 pgofs, whence)) { 463 f2fs_put_dnode(&dn); 464 goto found; 465 } 466 } 467 f2fs_put_dnode(&dn); 468 } 469 470 if (whence == SEEK_DATA) 471 goto fail; 472 found: 473 if (whence == SEEK_HOLE && data_ofs > isize) 474 data_ofs = isize; 475 inode_unlock(inode); 476 return vfs_setpos(file, data_ofs, maxbytes); 477 fail: 478 inode_unlock(inode); 479 return -ENXIO; 480 } 481 482 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence) 483 { 484 struct inode *inode = file->f_mapping->host; 485 loff_t maxbytes = inode->i_sb->s_maxbytes; 486 487 switch (whence) { 488 case SEEK_SET: 489 case SEEK_CUR: 490 case SEEK_END: 491 return generic_file_llseek_size(file, offset, whence, 492 maxbytes, i_size_read(inode)); 493 case SEEK_DATA: 494 case SEEK_HOLE: 495 if (offset < 0) 496 return -ENXIO; 497 return f2fs_seek_block(file, offset, whence); 498 } 499 500 return -EINVAL; 501 } 502 503 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) 504 { 505 struct inode *inode = file_inode(file); 506 int err; 507 508 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) 509 return -EIO; 510 511 if (!f2fs_is_compress_backend_ready(inode)) 512 return -EOPNOTSUPP; 513 514 /* we don't need to use inline_data strictly */ 515 err = f2fs_convert_inline_inode(inode); 516 if (err) 517 return err; 518 519 file_accessed(file); 520 vma->vm_ops = &f2fs_file_vm_ops; 521 set_inode_flag(inode, FI_MMAP_FILE); 522 return 0; 523 } 524 525 static int f2fs_file_open(struct inode *inode, struct file *filp) 526 { 527 int err = fscrypt_file_open(inode, filp); 528 529 if (err) 530 return err; 531 532 if (!f2fs_is_compress_backend_ready(inode)) 533 return -EOPNOTSUPP; 534 535 err = fsverity_file_open(inode, filp); 536 if (err) 537 return err; 538 539 filp->f_mode |= FMODE_NOWAIT; 540 541 return dquot_file_open(inode, filp); 542 } 543 544 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count) 545 { 546 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 547 struct f2fs_node *raw_node; 548 int nr_free = 0, ofs = dn->ofs_in_node, len = count; 549 __le32 *addr; 550 int base = 0; 551 bool compressed_cluster = false; 552 int cluster_index = 0, valid_blocks = 0; 553 int cluster_size = F2FS_I(dn->inode)->i_cluster_size; 554 555 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode)) 556 base = get_extra_isize(dn->inode); 557 558 raw_node = F2FS_NODE(dn->node_page); 559 addr = blkaddr_in_node(raw_node) + base + ofs; 560 561 /* Assumption: truncateion starts with cluster */ 562 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) { 563 block_t blkaddr = le32_to_cpu(*addr); 564 565 if (f2fs_compressed_file(dn->inode) && 566 !(cluster_index & (cluster_size - 1))) { 567 if (compressed_cluster) 568 f2fs_i_compr_blocks_update(dn->inode, 569 valid_blocks, false); 570 compressed_cluster = (blkaddr == COMPRESS_ADDR); 571 valid_blocks = 0; 572 } 573 574 if (blkaddr == NULL_ADDR) 575 continue; 576 577 dn->data_blkaddr = NULL_ADDR; 578 f2fs_set_data_blkaddr(dn); 579 580 if (__is_valid_data_blkaddr(blkaddr)) { 581 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, 582 DATA_GENERIC_ENHANCE)) 583 continue; 584 if (compressed_cluster) 585 valid_blocks++; 586 } 587 588 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page)) 589 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN); 590 591 f2fs_invalidate_blocks(sbi, blkaddr); 592 nr_free++; 593 } 594 595 if (compressed_cluster) 596 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false); 597 598 if (nr_free) { 599 pgoff_t fofs; 600 /* 601 * once we invalidate valid blkaddr in range [ofs, ofs + count], 602 * we will invalidate all blkaddr in the whole range. 603 */ 604 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), 605 dn->inode) + ofs; 606 f2fs_update_extent_cache_range(dn, fofs, 0, len); 607 dec_valid_block_count(sbi, dn->inode, nr_free); 608 } 609 dn->ofs_in_node = ofs; 610 611 f2fs_update_time(sbi, REQ_TIME); 612 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid, 613 dn->ofs_in_node, nr_free); 614 } 615 616 void f2fs_truncate_data_blocks(struct dnode_of_data *dn) 617 { 618 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode)); 619 } 620 621 static int truncate_partial_data_page(struct inode *inode, u64 from, 622 bool cache_only) 623 { 624 loff_t offset = from & (PAGE_SIZE - 1); 625 pgoff_t index = from >> PAGE_SHIFT; 626 struct address_space *mapping = inode->i_mapping; 627 struct page *page; 628 629 if (!offset && !cache_only) 630 return 0; 631 632 if (cache_only) { 633 page = find_lock_page(mapping, index); 634 if (page && PageUptodate(page)) 635 goto truncate_out; 636 f2fs_put_page(page, 1); 637 return 0; 638 } 639 640 if (f2fs_compressed_file(inode)) 641 return 0; 642 643 page = f2fs_get_lock_data_page(inode, index, true); 644 if (IS_ERR(page)) 645 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page); 646 truncate_out: 647 f2fs_wait_on_page_writeback(page, DATA, true, true); 648 zero_user(page, offset, PAGE_SIZE - offset); 649 650 /* An encrypted inode should have a key and truncate the last page. */ 651 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode)); 652 if (!cache_only) 653 set_page_dirty(page); 654 f2fs_put_page(page, 1); 655 return 0; 656 } 657 658 static int do_truncate_blocks(struct inode *inode, u64 from, bool lock) 659 { 660 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 661 struct dnode_of_data dn; 662 pgoff_t free_from; 663 int count = 0, err = 0; 664 struct page *ipage; 665 bool truncate_page = false; 666 667 trace_f2fs_truncate_blocks_enter(inode, from); 668 669 free_from = (pgoff_t)F2FS_BLK_ALIGN(from); 670 671 if (free_from >= sbi->max_file_blocks) 672 goto free_partial; 673 674 if (lock) 675 f2fs_lock_op(sbi); 676 677 ipage = f2fs_get_node_page(sbi, inode->i_ino); 678 if (IS_ERR(ipage)) { 679 err = PTR_ERR(ipage); 680 goto out; 681 } 682 683 if (f2fs_has_inline_data(inode)) { 684 f2fs_truncate_inline_inode(inode, ipage, from); 685 f2fs_put_page(ipage, 1); 686 truncate_page = true; 687 goto out; 688 } 689 690 set_new_dnode(&dn, inode, ipage, NULL, 0); 691 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA); 692 if (err) { 693 if (err == -ENOENT) 694 goto free_next; 695 goto out; 696 } 697 698 count = ADDRS_PER_PAGE(dn.node_page, inode); 699 700 count -= dn.ofs_in_node; 701 f2fs_bug_on(sbi, count < 0); 702 703 if (dn.ofs_in_node || IS_INODE(dn.node_page)) { 704 f2fs_truncate_data_blocks_range(&dn, count); 705 free_from += count; 706 } 707 708 f2fs_put_dnode(&dn); 709 free_next: 710 err = f2fs_truncate_inode_blocks(inode, free_from); 711 out: 712 if (lock) 713 f2fs_unlock_op(sbi); 714 free_partial: 715 /* lastly zero out the first data page */ 716 if (!err) 717 err = truncate_partial_data_page(inode, from, truncate_page); 718 719 trace_f2fs_truncate_blocks_exit(inode, err); 720 return err; 721 } 722 723 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock) 724 { 725 u64 free_from = from; 726 727 /* 728 * for compressed file, only support cluster size 729 * aligned truncation. 730 */ 731 if (f2fs_compressed_file(inode)) { 732 size_t cluster_shift = PAGE_SHIFT + 733 F2FS_I(inode)->i_log_cluster_size; 734 size_t cluster_mask = (1 << cluster_shift) - 1; 735 736 free_from = from >> cluster_shift; 737 if (from & cluster_mask) 738 free_from++; 739 free_from <<= cluster_shift; 740 } 741 742 return do_truncate_blocks(inode, free_from, lock); 743 } 744 745 int f2fs_truncate(struct inode *inode) 746 { 747 int err; 748 749 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) 750 return -EIO; 751 752 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 753 S_ISLNK(inode->i_mode))) 754 return 0; 755 756 trace_f2fs_truncate(inode); 757 758 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) { 759 f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE); 760 return -EIO; 761 } 762 763 /* we should check inline_data size */ 764 if (!f2fs_may_inline_data(inode)) { 765 err = f2fs_convert_inline_inode(inode); 766 if (err) 767 return err; 768 } 769 770 err = f2fs_truncate_blocks(inode, i_size_read(inode), true); 771 if (err) 772 return err; 773 774 inode->i_mtime = inode->i_ctime = current_time(inode); 775 f2fs_mark_inode_dirty_sync(inode, false); 776 return 0; 777 } 778 779 int f2fs_getattr(const struct path *path, struct kstat *stat, 780 u32 request_mask, unsigned int query_flags) 781 { 782 struct inode *inode = d_inode(path->dentry); 783 struct f2fs_inode_info *fi = F2FS_I(inode); 784 struct f2fs_inode *ri; 785 unsigned int flags; 786 787 if (f2fs_has_extra_attr(inode) && 788 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) && 789 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) { 790 stat->result_mask |= STATX_BTIME; 791 stat->btime.tv_sec = fi->i_crtime.tv_sec; 792 stat->btime.tv_nsec = fi->i_crtime.tv_nsec; 793 } 794 795 flags = fi->i_flags; 796 if (flags & F2FS_APPEND_FL) 797 stat->attributes |= STATX_ATTR_APPEND; 798 if (IS_ENCRYPTED(inode)) 799 stat->attributes |= STATX_ATTR_ENCRYPTED; 800 if (flags & F2FS_IMMUTABLE_FL) 801 stat->attributes |= STATX_ATTR_IMMUTABLE; 802 if (flags & F2FS_NODUMP_FL) 803 stat->attributes |= STATX_ATTR_NODUMP; 804 if (IS_VERITY(inode)) 805 stat->attributes |= STATX_ATTR_VERITY; 806 807 stat->attributes_mask |= (STATX_ATTR_APPEND | 808 STATX_ATTR_ENCRYPTED | 809 STATX_ATTR_IMMUTABLE | 810 STATX_ATTR_NODUMP | 811 STATX_ATTR_VERITY); 812 813 generic_fillattr(inode, stat); 814 815 /* we need to show initial sectors used for inline_data/dentries */ 816 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) || 817 f2fs_has_inline_dentry(inode)) 818 stat->blocks += (stat->size + 511) >> 9; 819 820 return 0; 821 } 822 823 #ifdef CONFIG_F2FS_FS_POSIX_ACL 824 static void __setattr_copy(struct inode *inode, const struct iattr *attr) 825 { 826 unsigned int ia_valid = attr->ia_valid; 827 828 if (ia_valid & ATTR_UID) 829 inode->i_uid = attr->ia_uid; 830 if (ia_valid & ATTR_GID) 831 inode->i_gid = attr->ia_gid; 832 if (ia_valid & ATTR_ATIME) 833 inode->i_atime = attr->ia_atime; 834 if (ia_valid & ATTR_MTIME) 835 inode->i_mtime = attr->ia_mtime; 836 if (ia_valid & ATTR_CTIME) 837 inode->i_ctime = attr->ia_ctime; 838 if (ia_valid & ATTR_MODE) { 839 umode_t mode = attr->ia_mode; 840 841 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) 842 mode &= ~S_ISGID; 843 set_acl_inode(inode, mode); 844 } 845 } 846 #else 847 #define __setattr_copy setattr_copy 848 #endif 849 850 int f2fs_setattr(struct dentry *dentry, struct iattr *attr) 851 { 852 struct inode *inode = d_inode(dentry); 853 int err; 854 855 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) 856 return -EIO; 857 858 if ((attr->ia_valid & ATTR_SIZE) && 859 !f2fs_is_compress_backend_ready(inode)) 860 return -EOPNOTSUPP; 861 862 err = setattr_prepare(dentry, attr); 863 if (err) 864 return err; 865 866 err = fscrypt_prepare_setattr(dentry, attr); 867 if (err) 868 return err; 869 870 err = fsverity_prepare_setattr(dentry, attr); 871 if (err) 872 return err; 873 874 if (is_quota_modification(inode, attr)) { 875 err = dquot_initialize(inode); 876 if (err) 877 return err; 878 } 879 if ((attr->ia_valid & ATTR_UID && 880 !uid_eq(attr->ia_uid, inode->i_uid)) || 881 (attr->ia_valid & ATTR_GID && 882 !gid_eq(attr->ia_gid, inode->i_gid))) { 883 f2fs_lock_op(F2FS_I_SB(inode)); 884 err = dquot_transfer(inode, attr); 885 if (err) { 886 set_sbi_flag(F2FS_I_SB(inode), 887 SBI_QUOTA_NEED_REPAIR); 888 f2fs_unlock_op(F2FS_I_SB(inode)); 889 return err; 890 } 891 /* 892 * update uid/gid under lock_op(), so that dquot and inode can 893 * be updated atomically. 894 */ 895 if (attr->ia_valid & ATTR_UID) 896 inode->i_uid = attr->ia_uid; 897 if (attr->ia_valid & ATTR_GID) 898 inode->i_gid = attr->ia_gid; 899 f2fs_mark_inode_dirty_sync(inode, true); 900 f2fs_unlock_op(F2FS_I_SB(inode)); 901 } 902 903 if (attr->ia_valid & ATTR_SIZE) { 904 loff_t old_size = i_size_read(inode); 905 906 if (attr->ia_size > MAX_INLINE_DATA(inode)) { 907 /* 908 * should convert inline inode before i_size_write to 909 * keep smaller than inline_data size with inline flag. 910 */ 911 err = f2fs_convert_inline_inode(inode); 912 if (err) 913 return err; 914 } 915 916 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 917 down_write(&F2FS_I(inode)->i_mmap_sem); 918 919 truncate_setsize(inode, attr->ia_size); 920 921 if (attr->ia_size <= old_size) 922 err = f2fs_truncate(inode); 923 /* 924 * do not trim all blocks after i_size if target size is 925 * larger than i_size. 926 */ 927 up_write(&F2FS_I(inode)->i_mmap_sem); 928 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 929 if (err) 930 return err; 931 932 down_write(&F2FS_I(inode)->i_sem); 933 inode->i_mtime = inode->i_ctime = current_time(inode); 934 F2FS_I(inode)->last_disk_size = i_size_read(inode); 935 up_write(&F2FS_I(inode)->i_sem); 936 } 937 938 __setattr_copy(inode, attr); 939 940 if (attr->ia_valid & ATTR_MODE) { 941 err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode)); 942 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) { 943 inode->i_mode = F2FS_I(inode)->i_acl_mode; 944 clear_inode_flag(inode, FI_ACL_MODE); 945 } 946 } 947 948 /* file size may changed here */ 949 f2fs_mark_inode_dirty_sync(inode, true); 950 951 /* inode change will produce dirty node pages flushed by checkpoint */ 952 f2fs_balance_fs(F2FS_I_SB(inode), true); 953 954 return err; 955 } 956 957 const struct inode_operations f2fs_file_inode_operations = { 958 .getattr = f2fs_getattr, 959 .setattr = f2fs_setattr, 960 .get_acl = f2fs_get_acl, 961 .set_acl = f2fs_set_acl, 962 #ifdef CONFIG_F2FS_FS_XATTR 963 .listxattr = f2fs_listxattr, 964 #endif 965 .fiemap = f2fs_fiemap, 966 }; 967 968 static int fill_zero(struct inode *inode, pgoff_t index, 969 loff_t start, loff_t len) 970 { 971 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 972 struct page *page; 973 974 if (!len) 975 return 0; 976 977 f2fs_balance_fs(sbi, true); 978 979 f2fs_lock_op(sbi); 980 page = f2fs_get_new_data_page(inode, NULL, index, false); 981 f2fs_unlock_op(sbi); 982 983 if (IS_ERR(page)) 984 return PTR_ERR(page); 985 986 f2fs_wait_on_page_writeback(page, DATA, true, true); 987 zero_user(page, start, len); 988 set_page_dirty(page); 989 f2fs_put_page(page, 1); 990 return 0; 991 } 992 993 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) 994 { 995 int err; 996 997 while (pg_start < pg_end) { 998 struct dnode_of_data dn; 999 pgoff_t end_offset, count; 1000 1001 set_new_dnode(&dn, inode, NULL, NULL, 0); 1002 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE); 1003 if (err) { 1004 if (err == -ENOENT) { 1005 pg_start = f2fs_get_next_page_offset(&dn, 1006 pg_start); 1007 continue; 1008 } 1009 return err; 1010 } 1011 1012 end_offset = ADDRS_PER_PAGE(dn.node_page, inode); 1013 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start); 1014 1015 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset); 1016 1017 f2fs_truncate_data_blocks_range(&dn, count); 1018 f2fs_put_dnode(&dn); 1019 1020 pg_start += count; 1021 } 1022 return 0; 1023 } 1024 1025 static int punch_hole(struct inode *inode, loff_t offset, loff_t len) 1026 { 1027 pgoff_t pg_start, pg_end; 1028 loff_t off_start, off_end; 1029 int ret; 1030 1031 ret = f2fs_convert_inline_inode(inode); 1032 if (ret) 1033 return ret; 1034 1035 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT; 1036 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT; 1037 1038 off_start = offset & (PAGE_SIZE - 1); 1039 off_end = (offset + len) & (PAGE_SIZE - 1); 1040 1041 if (pg_start == pg_end) { 1042 ret = fill_zero(inode, pg_start, off_start, 1043 off_end - off_start); 1044 if (ret) 1045 return ret; 1046 } else { 1047 if (off_start) { 1048 ret = fill_zero(inode, pg_start++, off_start, 1049 PAGE_SIZE - off_start); 1050 if (ret) 1051 return ret; 1052 } 1053 if (off_end) { 1054 ret = fill_zero(inode, pg_end, 0, off_end); 1055 if (ret) 1056 return ret; 1057 } 1058 1059 if (pg_start < pg_end) { 1060 struct address_space *mapping = inode->i_mapping; 1061 loff_t blk_start, blk_end; 1062 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1063 1064 f2fs_balance_fs(sbi, true); 1065 1066 blk_start = (loff_t)pg_start << PAGE_SHIFT; 1067 blk_end = (loff_t)pg_end << PAGE_SHIFT; 1068 1069 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 1070 down_write(&F2FS_I(inode)->i_mmap_sem); 1071 1072 truncate_inode_pages_range(mapping, blk_start, 1073 blk_end - 1); 1074 1075 f2fs_lock_op(sbi); 1076 ret = f2fs_truncate_hole(inode, pg_start, pg_end); 1077 f2fs_unlock_op(sbi); 1078 1079 up_write(&F2FS_I(inode)->i_mmap_sem); 1080 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 1081 } 1082 } 1083 1084 return ret; 1085 } 1086 1087 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr, 1088 int *do_replace, pgoff_t off, pgoff_t len) 1089 { 1090 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1091 struct dnode_of_data dn; 1092 int ret, done, i; 1093 1094 next_dnode: 1095 set_new_dnode(&dn, inode, NULL, NULL, 0); 1096 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA); 1097 if (ret && ret != -ENOENT) { 1098 return ret; 1099 } else if (ret == -ENOENT) { 1100 if (dn.max_level == 0) 1101 return -ENOENT; 1102 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) - 1103 dn.ofs_in_node, len); 1104 blkaddr += done; 1105 do_replace += done; 1106 goto next; 1107 } 1108 1109 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) - 1110 dn.ofs_in_node, len); 1111 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) { 1112 *blkaddr = datablock_addr(dn.inode, 1113 dn.node_page, dn.ofs_in_node); 1114 1115 if (__is_valid_data_blkaddr(*blkaddr) && 1116 !f2fs_is_valid_blkaddr(sbi, *blkaddr, 1117 DATA_GENERIC_ENHANCE)) { 1118 f2fs_put_dnode(&dn); 1119 return -EFSCORRUPTED; 1120 } 1121 1122 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) { 1123 1124 if (test_opt(sbi, LFS)) { 1125 f2fs_put_dnode(&dn); 1126 return -EOPNOTSUPP; 1127 } 1128 1129 /* do not invalidate this block address */ 1130 f2fs_update_data_blkaddr(&dn, NULL_ADDR); 1131 *do_replace = 1; 1132 } 1133 } 1134 f2fs_put_dnode(&dn); 1135 next: 1136 len -= done; 1137 off += done; 1138 if (len) 1139 goto next_dnode; 1140 return 0; 1141 } 1142 1143 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr, 1144 int *do_replace, pgoff_t off, int len) 1145 { 1146 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1147 struct dnode_of_data dn; 1148 int ret, i; 1149 1150 for (i = 0; i < len; i++, do_replace++, blkaddr++) { 1151 if (*do_replace == 0) 1152 continue; 1153 1154 set_new_dnode(&dn, inode, NULL, NULL, 0); 1155 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA); 1156 if (ret) { 1157 dec_valid_block_count(sbi, inode, 1); 1158 f2fs_invalidate_blocks(sbi, *blkaddr); 1159 } else { 1160 f2fs_update_data_blkaddr(&dn, *blkaddr); 1161 } 1162 f2fs_put_dnode(&dn); 1163 } 1164 return 0; 1165 } 1166 1167 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode, 1168 block_t *blkaddr, int *do_replace, 1169 pgoff_t src, pgoff_t dst, pgoff_t len, bool full) 1170 { 1171 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode); 1172 pgoff_t i = 0; 1173 int ret; 1174 1175 while (i < len) { 1176 if (blkaddr[i] == NULL_ADDR && !full) { 1177 i++; 1178 continue; 1179 } 1180 1181 if (do_replace[i] || blkaddr[i] == NULL_ADDR) { 1182 struct dnode_of_data dn; 1183 struct node_info ni; 1184 size_t new_size; 1185 pgoff_t ilen; 1186 1187 set_new_dnode(&dn, dst_inode, NULL, NULL, 0); 1188 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE); 1189 if (ret) 1190 return ret; 1191 1192 ret = f2fs_get_node_info(sbi, dn.nid, &ni); 1193 if (ret) { 1194 f2fs_put_dnode(&dn); 1195 return ret; 1196 } 1197 1198 ilen = min((pgoff_t) 1199 ADDRS_PER_PAGE(dn.node_page, dst_inode) - 1200 dn.ofs_in_node, len - i); 1201 do { 1202 dn.data_blkaddr = datablock_addr(dn.inode, 1203 dn.node_page, dn.ofs_in_node); 1204 f2fs_truncate_data_blocks_range(&dn, 1); 1205 1206 if (do_replace[i]) { 1207 f2fs_i_blocks_write(src_inode, 1208 1, false, false); 1209 f2fs_i_blocks_write(dst_inode, 1210 1, true, false); 1211 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, 1212 blkaddr[i], ni.version, true, false); 1213 1214 do_replace[i] = 0; 1215 } 1216 dn.ofs_in_node++; 1217 i++; 1218 new_size = (loff_t)(dst + i) << PAGE_SHIFT; 1219 if (dst_inode->i_size < new_size) 1220 f2fs_i_size_write(dst_inode, new_size); 1221 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR)); 1222 1223 f2fs_put_dnode(&dn); 1224 } else { 1225 struct page *psrc, *pdst; 1226 1227 psrc = f2fs_get_lock_data_page(src_inode, 1228 src + i, true); 1229 if (IS_ERR(psrc)) 1230 return PTR_ERR(psrc); 1231 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i, 1232 true); 1233 if (IS_ERR(pdst)) { 1234 f2fs_put_page(psrc, 1); 1235 return PTR_ERR(pdst); 1236 } 1237 f2fs_copy_page(psrc, pdst); 1238 set_page_dirty(pdst); 1239 f2fs_put_page(pdst, 1); 1240 f2fs_put_page(psrc, 1); 1241 1242 ret = f2fs_truncate_hole(src_inode, 1243 src + i, src + i + 1); 1244 if (ret) 1245 return ret; 1246 i++; 1247 } 1248 } 1249 return 0; 1250 } 1251 1252 static int __exchange_data_block(struct inode *src_inode, 1253 struct inode *dst_inode, pgoff_t src, pgoff_t dst, 1254 pgoff_t len, bool full) 1255 { 1256 block_t *src_blkaddr; 1257 int *do_replace; 1258 pgoff_t olen; 1259 int ret; 1260 1261 while (len) { 1262 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len); 1263 1264 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode), 1265 array_size(olen, sizeof(block_t)), 1266 GFP_NOFS); 1267 if (!src_blkaddr) 1268 return -ENOMEM; 1269 1270 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode), 1271 array_size(olen, sizeof(int)), 1272 GFP_NOFS); 1273 if (!do_replace) { 1274 kvfree(src_blkaddr); 1275 return -ENOMEM; 1276 } 1277 1278 ret = __read_out_blkaddrs(src_inode, src_blkaddr, 1279 do_replace, src, olen); 1280 if (ret) 1281 goto roll_back; 1282 1283 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr, 1284 do_replace, src, dst, olen, full); 1285 if (ret) 1286 goto roll_back; 1287 1288 src += olen; 1289 dst += olen; 1290 len -= olen; 1291 1292 kvfree(src_blkaddr); 1293 kvfree(do_replace); 1294 } 1295 return 0; 1296 1297 roll_back: 1298 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen); 1299 kvfree(src_blkaddr); 1300 kvfree(do_replace); 1301 return ret; 1302 } 1303 1304 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len) 1305 { 1306 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1307 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 1308 pgoff_t start = offset >> PAGE_SHIFT; 1309 pgoff_t end = (offset + len) >> PAGE_SHIFT; 1310 int ret; 1311 1312 f2fs_balance_fs(sbi, true); 1313 1314 /* avoid gc operation during block exchange */ 1315 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 1316 down_write(&F2FS_I(inode)->i_mmap_sem); 1317 1318 f2fs_lock_op(sbi); 1319 f2fs_drop_extent_tree(inode); 1320 truncate_pagecache(inode, offset); 1321 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true); 1322 f2fs_unlock_op(sbi); 1323 1324 up_write(&F2FS_I(inode)->i_mmap_sem); 1325 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 1326 return ret; 1327 } 1328 1329 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len) 1330 { 1331 loff_t new_size; 1332 int ret; 1333 1334 if (offset + len >= i_size_read(inode)) 1335 return -EINVAL; 1336 1337 /* collapse range should be aligned to block size of f2fs. */ 1338 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) 1339 return -EINVAL; 1340 1341 ret = f2fs_convert_inline_inode(inode); 1342 if (ret) 1343 return ret; 1344 1345 /* write out all dirty pages from offset */ 1346 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 1347 if (ret) 1348 return ret; 1349 1350 ret = f2fs_do_collapse(inode, offset, len); 1351 if (ret) 1352 return ret; 1353 1354 /* write out all moved pages, if possible */ 1355 down_write(&F2FS_I(inode)->i_mmap_sem); 1356 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 1357 truncate_pagecache(inode, offset); 1358 1359 new_size = i_size_read(inode) - len; 1360 truncate_pagecache(inode, new_size); 1361 1362 ret = f2fs_truncate_blocks(inode, new_size, true); 1363 up_write(&F2FS_I(inode)->i_mmap_sem); 1364 if (!ret) 1365 f2fs_i_size_write(inode, new_size); 1366 return ret; 1367 } 1368 1369 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start, 1370 pgoff_t end) 1371 { 1372 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 1373 pgoff_t index = start; 1374 unsigned int ofs_in_node = dn->ofs_in_node; 1375 blkcnt_t count = 0; 1376 int ret; 1377 1378 for (; index < end; index++, dn->ofs_in_node++) { 1379 if (datablock_addr(dn->inode, dn->node_page, 1380 dn->ofs_in_node) == NULL_ADDR) 1381 count++; 1382 } 1383 1384 dn->ofs_in_node = ofs_in_node; 1385 ret = f2fs_reserve_new_blocks(dn, count); 1386 if (ret) 1387 return ret; 1388 1389 dn->ofs_in_node = ofs_in_node; 1390 for (index = start; index < end; index++, dn->ofs_in_node++) { 1391 dn->data_blkaddr = datablock_addr(dn->inode, 1392 dn->node_page, dn->ofs_in_node); 1393 /* 1394 * f2fs_reserve_new_blocks will not guarantee entire block 1395 * allocation. 1396 */ 1397 if (dn->data_blkaddr == NULL_ADDR) { 1398 ret = -ENOSPC; 1399 break; 1400 } 1401 if (dn->data_blkaddr != NEW_ADDR) { 1402 f2fs_invalidate_blocks(sbi, dn->data_blkaddr); 1403 dn->data_blkaddr = NEW_ADDR; 1404 f2fs_set_data_blkaddr(dn); 1405 } 1406 } 1407 1408 f2fs_update_extent_cache_range(dn, start, 0, index - start); 1409 1410 return ret; 1411 } 1412 1413 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len, 1414 int mode) 1415 { 1416 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1417 struct address_space *mapping = inode->i_mapping; 1418 pgoff_t index, pg_start, pg_end; 1419 loff_t new_size = i_size_read(inode); 1420 loff_t off_start, off_end; 1421 int ret = 0; 1422 1423 ret = inode_newsize_ok(inode, (len + offset)); 1424 if (ret) 1425 return ret; 1426 1427 ret = f2fs_convert_inline_inode(inode); 1428 if (ret) 1429 return ret; 1430 1431 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1); 1432 if (ret) 1433 return ret; 1434 1435 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT; 1436 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT; 1437 1438 off_start = offset & (PAGE_SIZE - 1); 1439 off_end = (offset + len) & (PAGE_SIZE - 1); 1440 1441 if (pg_start == pg_end) { 1442 ret = fill_zero(inode, pg_start, off_start, 1443 off_end - off_start); 1444 if (ret) 1445 return ret; 1446 1447 new_size = max_t(loff_t, new_size, offset + len); 1448 } else { 1449 if (off_start) { 1450 ret = fill_zero(inode, pg_start++, off_start, 1451 PAGE_SIZE - off_start); 1452 if (ret) 1453 return ret; 1454 1455 new_size = max_t(loff_t, new_size, 1456 (loff_t)pg_start << PAGE_SHIFT); 1457 } 1458 1459 for (index = pg_start; index < pg_end;) { 1460 struct dnode_of_data dn; 1461 unsigned int end_offset; 1462 pgoff_t end; 1463 1464 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 1465 down_write(&F2FS_I(inode)->i_mmap_sem); 1466 1467 truncate_pagecache_range(inode, 1468 (loff_t)index << PAGE_SHIFT, 1469 ((loff_t)pg_end << PAGE_SHIFT) - 1); 1470 1471 f2fs_lock_op(sbi); 1472 1473 set_new_dnode(&dn, inode, NULL, NULL, 0); 1474 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE); 1475 if (ret) { 1476 f2fs_unlock_op(sbi); 1477 up_write(&F2FS_I(inode)->i_mmap_sem); 1478 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 1479 goto out; 1480 } 1481 1482 end_offset = ADDRS_PER_PAGE(dn.node_page, inode); 1483 end = min(pg_end, end_offset - dn.ofs_in_node + index); 1484 1485 ret = f2fs_do_zero_range(&dn, index, end); 1486 f2fs_put_dnode(&dn); 1487 1488 f2fs_unlock_op(sbi); 1489 up_write(&F2FS_I(inode)->i_mmap_sem); 1490 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 1491 1492 f2fs_balance_fs(sbi, dn.node_changed); 1493 1494 if (ret) 1495 goto out; 1496 1497 index = end; 1498 new_size = max_t(loff_t, new_size, 1499 (loff_t)index << PAGE_SHIFT); 1500 } 1501 1502 if (off_end) { 1503 ret = fill_zero(inode, pg_end, 0, off_end); 1504 if (ret) 1505 goto out; 1506 1507 new_size = max_t(loff_t, new_size, offset + len); 1508 } 1509 } 1510 1511 out: 1512 if (new_size > i_size_read(inode)) { 1513 if (mode & FALLOC_FL_KEEP_SIZE) 1514 file_set_keep_isize(inode); 1515 else 1516 f2fs_i_size_write(inode, new_size); 1517 } 1518 return ret; 1519 } 1520 1521 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len) 1522 { 1523 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1524 pgoff_t nr, pg_start, pg_end, delta, idx; 1525 loff_t new_size; 1526 int ret = 0; 1527 1528 new_size = i_size_read(inode) + len; 1529 ret = inode_newsize_ok(inode, new_size); 1530 if (ret) 1531 return ret; 1532 1533 if (offset >= i_size_read(inode)) 1534 return -EINVAL; 1535 1536 /* insert range should be aligned to block size of f2fs. */ 1537 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) 1538 return -EINVAL; 1539 1540 ret = f2fs_convert_inline_inode(inode); 1541 if (ret) 1542 return ret; 1543 1544 f2fs_balance_fs(sbi, true); 1545 1546 down_write(&F2FS_I(inode)->i_mmap_sem); 1547 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true); 1548 up_write(&F2FS_I(inode)->i_mmap_sem); 1549 if (ret) 1550 return ret; 1551 1552 /* write out all dirty pages from offset */ 1553 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 1554 if (ret) 1555 return ret; 1556 1557 pg_start = offset >> PAGE_SHIFT; 1558 pg_end = (offset + len) >> PAGE_SHIFT; 1559 delta = pg_end - pg_start; 1560 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 1561 1562 /* avoid gc operation during block exchange */ 1563 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 1564 down_write(&F2FS_I(inode)->i_mmap_sem); 1565 truncate_pagecache(inode, offset); 1566 1567 while (!ret && idx > pg_start) { 1568 nr = idx - pg_start; 1569 if (nr > delta) 1570 nr = delta; 1571 idx -= nr; 1572 1573 f2fs_lock_op(sbi); 1574 f2fs_drop_extent_tree(inode); 1575 1576 ret = __exchange_data_block(inode, inode, idx, 1577 idx + delta, nr, false); 1578 f2fs_unlock_op(sbi); 1579 } 1580 up_write(&F2FS_I(inode)->i_mmap_sem); 1581 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 1582 1583 /* write out all moved pages, if possible */ 1584 down_write(&F2FS_I(inode)->i_mmap_sem); 1585 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 1586 truncate_pagecache(inode, offset); 1587 up_write(&F2FS_I(inode)->i_mmap_sem); 1588 1589 if (!ret) 1590 f2fs_i_size_write(inode, new_size); 1591 return ret; 1592 } 1593 1594 static int expand_inode_data(struct inode *inode, loff_t offset, 1595 loff_t len, int mode) 1596 { 1597 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1598 struct f2fs_map_blocks map = { .m_next_pgofs = NULL, 1599 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE, 1600 .m_may_create = true }; 1601 pgoff_t pg_end; 1602 loff_t new_size = i_size_read(inode); 1603 loff_t off_end; 1604 int err; 1605 1606 err = inode_newsize_ok(inode, (len + offset)); 1607 if (err) 1608 return err; 1609 1610 err = f2fs_convert_inline_inode(inode); 1611 if (err) 1612 return err; 1613 1614 f2fs_balance_fs(sbi, true); 1615 1616 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT; 1617 off_end = (offset + len) & (PAGE_SIZE - 1); 1618 1619 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT; 1620 map.m_len = pg_end - map.m_lblk; 1621 if (off_end) 1622 map.m_len++; 1623 1624 if (!map.m_len) 1625 return 0; 1626 1627 if (f2fs_is_pinned_file(inode)) { 1628 block_t len = (map.m_len >> sbi->log_blocks_per_seg) << 1629 sbi->log_blocks_per_seg; 1630 block_t done = 0; 1631 1632 if (map.m_len % sbi->blocks_per_seg) 1633 len += sbi->blocks_per_seg; 1634 1635 map.m_len = sbi->blocks_per_seg; 1636 next_alloc: 1637 if (has_not_enough_free_secs(sbi, 0, 1638 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) { 1639 down_write(&sbi->gc_lock); 1640 err = f2fs_gc(sbi, true, false, NULL_SEGNO); 1641 if (err && err != -ENODATA && err != -EAGAIN) 1642 goto out_err; 1643 } 1644 1645 down_write(&sbi->pin_sem); 1646 map.m_seg_type = CURSEG_COLD_DATA_PINNED; 1647 f2fs_allocate_new_segments(sbi, CURSEG_COLD_DATA); 1648 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO); 1649 up_write(&sbi->pin_sem); 1650 1651 done += map.m_len; 1652 len -= map.m_len; 1653 map.m_lblk += map.m_len; 1654 if (!err && len) 1655 goto next_alloc; 1656 1657 map.m_len = done; 1658 } else { 1659 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO); 1660 } 1661 out_err: 1662 if (err) { 1663 pgoff_t last_off; 1664 1665 if (!map.m_len) 1666 return err; 1667 1668 last_off = map.m_lblk + map.m_len - 1; 1669 1670 /* update new size to the failed position */ 1671 new_size = (last_off == pg_end) ? offset + len : 1672 (loff_t)(last_off + 1) << PAGE_SHIFT; 1673 } else { 1674 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end; 1675 } 1676 1677 if (new_size > i_size_read(inode)) { 1678 if (mode & FALLOC_FL_KEEP_SIZE) 1679 file_set_keep_isize(inode); 1680 else 1681 f2fs_i_size_write(inode, new_size); 1682 } 1683 1684 return err; 1685 } 1686 1687 static long f2fs_fallocate(struct file *file, int mode, 1688 loff_t offset, loff_t len) 1689 { 1690 struct inode *inode = file_inode(file); 1691 long ret = 0; 1692 1693 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) 1694 return -EIO; 1695 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode))) 1696 return -ENOSPC; 1697 if (!f2fs_is_compress_backend_ready(inode)) 1698 return -EOPNOTSUPP; 1699 1700 /* f2fs only support ->fallocate for regular file */ 1701 if (!S_ISREG(inode->i_mode)) 1702 return -EINVAL; 1703 1704 if (IS_ENCRYPTED(inode) && 1705 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) 1706 return -EOPNOTSUPP; 1707 1708 if (f2fs_compressed_file(inode) && 1709 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE | 1710 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) 1711 return -EOPNOTSUPP; 1712 1713 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | 1714 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | 1715 FALLOC_FL_INSERT_RANGE)) 1716 return -EOPNOTSUPP; 1717 1718 inode_lock(inode); 1719 1720 if (mode & FALLOC_FL_PUNCH_HOLE) { 1721 if (offset >= inode->i_size) 1722 goto out; 1723 1724 ret = punch_hole(inode, offset, len); 1725 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { 1726 ret = f2fs_collapse_range(inode, offset, len); 1727 } else if (mode & FALLOC_FL_ZERO_RANGE) { 1728 ret = f2fs_zero_range(inode, offset, len, mode); 1729 } else if (mode & FALLOC_FL_INSERT_RANGE) { 1730 ret = f2fs_insert_range(inode, offset, len); 1731 } else { 1732 ret = expand_inode_data(inode, offset, len, mode); 1733 } 1734 1735 if (!ret) { 1736 inode->i_mtime = inode->i_ctime = current_time(inode); 1737 f2fs_mark_inode_dirty_sync(inode, false); 1738 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1739 } 1740 1741 out: 1742 inode_unlock(inode); 1743 1744 trace_f2fs_fallocate(inode, mode, offset, len, ret); 1745 return ret; 1746 } 1747 1748 static int f2fs_release_file(struct inode *inode, struct file *filp) 1749 { 1750 /* 1751 * f2fs_relase_file is called at every close calls. So we should 1752 * not drop any inmemory pages by close called by other process. 1753 */ 1754 if (!(filp->f_mode & FMODE_WRITE) || 1755 atomic_read(&inode->i_writecount) != 1) 1756 return 0; 1757 1758 /* some remained atomic pages should discarded */ 1759 if (f2fs_is_atomic_file(inode)) 1760 f2fs_drop_inmem_pages(inode); 1761 if (f2fs_is_volatile_file(inode)) { 1762 set_inode_flag(inode, FI_DROP_CACHE); 1763 filemap_fdatawrite(inode->i_mapping); 1764 clear_inode_flag(inode, FI_DROP_CACHE); 1765 clear_inode_flag(inode, FI_VOLATILE_FILE); 1766 stat_dec_volatile_write(inode); 1767 } 1768 return 0; 1769 } 1770 1771 static int f2fs_file_flush(struct file *file, fl_owner_t id) 1772 { 1773 struct inode *inode = file_inode(file); 1774 1775 /* 1776 * If the process doing a transaction is crashed, we should do 1777 * roll-back. Otherwise, other reader/write can see corrupted database 1778 * until all the writers close its file. Since this should be done 1779 * before dropping file lock, it needs to do in ->flush. 1780 */ 1781 if (f2fs_is_atomic_file(inode) && 1782 F2FS_I(inode)->inmem_task == current) 1783 f2fs_drop_inmem_pages(inode); 1784 return 0; 1785 } 1786 1787 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask) 1788 { 1789 struct f2fs_inode_info *fi = F2FS_I(inode); 1790 1791 /* Is it quota file? Do not allow user to mess with it */ 1792 if (IS_NOQUOTA(inode)) 1793 return -EPERM; 1794 1795 if ((iflags ^ fi->i_flags) & F2FS_CASEFOLD_FL) { 1796 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode))) 1797 return -EOPNOTSUPP; 1798 if (!f2fs_empty_dir(inode)) 1799 return -ENOTEMPTY; 1800 } 1801 1802 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) { 1803 if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) 1804 return -EOPNOTSUPP; 1805 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL)) 1806 return -EINVAL; 1807 } 1808 1809 if ((iflags ^ fi->i_flags) & F2FS_COMPR_FL) { 1810 if (S_ISREG(inode->i_mode) && 1811 (fi->i_flags & F2FS_COMPR_FL || i_size_read(inode) || 1812 F2FS_HAS_BLOCKS(inode))) 1813 return -EINVAL; 1814 if (iflags & F2FS_NOCOMP_FL) 1815 return -EINVAL; 1816 if (iflags & F2FS_COMPR_FL) { 1817 int err = f2fs_convert_inline_inode(inode); 1818 1819 if (err) 1820 return err; 1821 1822 if (!f2fs_may_compress(inode)) 1823 return -EINVAL; 1824 1825 set_compress_context(inode); 1826 } 1827 } 1828 if ((iflags ^ fi->i_flags) & F2FS_NOCOMP_FL) { 1829 if (fi->i_flags & F2FS_COMPR_FL) 1830 return -EINVAL; 1831 } 1832 1833 fi->i_flags = iflags | (fi->i_flags & ~mask); 1834 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) && 1835 (fi->i_flags & F2FS_NOCOMP_FL)); 1836 1837 if (fi->i_flags & F2FS_PROJINHERIT_FL) 1838 set_inode_flag(inode, FI_PROJ_INHERIT); 1839 else 1840 clear_inode_flag(inode, FI_PROJ_INHERIT); 1841 1842 inode->i_ctime = current_time(inode); 1843 f2fs_set_inode_flags(inode); 1844 f2fs_mark_inode_dirty_sync(inode, true); 1845 return 0; 1846 } 1847 1848 /* FS_IOC_GETFLAGS and FS_IOC_SETFLAGS support */ 1849 1850 /* 1851 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry 1852 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to 1853 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add 1854 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL. 1855 */ 1856 1857 static const struct { 1858 u32 iflag; 1859 u32 fsflag; 1860 } f2fs_fsflags_map[] = { 1861 { F2FS_COMPR_FL, FS_COMPR_FL }, 1862 { F2FS_SYNC_FL, FS_SYNC_FL }, 1863 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL }, 1864 { F2FS_APPEND_FL, FS_APPEND_FL }, 1865 { F2FS_NODUMP_FL, FS_NODUMP_FL }, 1866 { F2FS_NOATIME_FL, FS_NOATIME_FL }, 1867 { F2FS_NOCOMP_FL, FS_NOCOMP_FL }, 1868 { F2FS_INDEX_FL, FS_INDEX_FL }, 1869 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL }, 1870 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL }, 1871 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL }, 1872 }; 1873 1874 #define F2FS_GETTABLE_FS_FL ( \ 1875 FS_COMPR_FL | \ 1876 FS_SYNC_FL | \ 1877 FS_IMMUTABLE_FL | \ 1878 FS_APPEND_FL | \ 1879 FS_NODUMP_FL | \ 1880 FS_NOATIME_FL | \ 1881 FS_NOCOMP_FL | \ 1882 FS_INDEX_FL | \ 1883 FS_DIRSYNC_FL | \ 1884 FS_PROJINHERIT_FL | \ 1885 FS_ENCRYPT_FL | \ 1886 FS_INLINE_DATA_FL | \ 1887 FS_NOCOW_FL | \ 1888 FS_VERITY_FL | \ 1889 FS_CASEFOLD_FL) 1890 1891 #define F2FS_SETTABLE_FS_FL ( \ 1892 FS_COMPR_FL | \ 1893 FS_SYNC_FL | \ 1894 FS_IMMUTABLE_FL | \ 1895 FS_APPEND_FL | \ 1896 FS_NODUMP_FL | \ 1897 FS_NOATIME_FL | \ 1898 FS_NOCOMP_FL | \ 1899 FS_DIRSYNC_FL | \ 1900 FS_PROJINHERIT_FL | \ 1901 FS_CASEFOLD_FL) 1902 1903 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */ 1904 static inline u32 f2fs_iflags_to_fsflags(u32 iflags) 1905 { 1906 u32 fsflags = 0; 1907 int i; 1908 1909 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++) 1910 if (iflags & f2fs_fsflags_map[i].iflag) 1911 fsflags |= f2fs_fsflags_map[i].fsflag; 1912 1913 return fsflags; 1914 } 1915 1916 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */ 1917 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags) 1918 { 1919 u32 iflags = 0; 1920 int i; 1921 1922 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++) 1923 if (fsflags & f2fs_fsflags_map[i].fsflag) 1924 iflags |= f2fs_fsflags_map[i].iflag; 1925 1926 return iflags; 1927 } 1928 1929 static int f2fs_ioc_getflags(struct file *filp, unsigned long arg) 1930 { 1931 struct inode *inode = file_inode(filp); 1932 struct f2fs_inode_info *fi = F2FS_I(inode); 1933 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags); 1934 1935 if (IS_ENCRYPTED(inode)) 1936 fsflags |= FS_ENCRYPT_FL; 1937 if (IS_VERITY(inode)) 1938 fsflags |= FS_VERITY_FL; 1939 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) 1940 fsflags |= FS_INLINE_DATA_FL; 1941 if (is_inode_flag_set(inode, FI_PIN_FILE)) 1942 fsflags |= FS_NOCOW_FL; 1943 1944 fsflags &= F2FS_GETTABLE_FS_FL; 1945 1946 return put_user(fsflags, (int __user *)arg); 1947 } 1948 1949 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg) 1950 { 1951 struct inode *inode = file_inode(filp); 1952 struct f2fs_inode_info *fi = F2FS_I(inode); 1953 u32 fsflags, old_fsflags; 1954 u32 iflags; 1955 int ret; 1956 1957 if (!inode_owner_or_capable(inode)) 1958 return -EACCES; 1959 1960 if (get_user(fsflags, (int __user *)arg)) 1961 return -EFAULT; 1962 1963 if (fsflags & ~F2FS_GETTABLE_FS_FL) 1964 return -EOPNOTSUPP; 1965 fsflags &= F2FS_SETTABLE_FS_FL; 1966 1967 iflags = f2fs_fsflags_to_iflags(fsflags); 1968 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags) 1969 return -EOPNOTSUPP; 1970 1971 ret = mnt_want_write_file(filp); 1972 if (ret) 1973 return ret; 1974 1975 inode_lock(inode); 1976 1977 old_fsflags = f2fs_iflags_to_fsflags(fi->i_flags); 1978 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags); 1979 if (ret) 1980 goto out; 1981 1982 ret = f2fs_setflags_common(inode, iflags, 1983 f2fs_fsflags_to_iflags(F2FS_SETTABLE_FS_FL)); 1984 out: 1985 inode_unlock(inode); 1986 mnt_drop_write_file(filp); 1987 return ret; 1988 } 1989 1990 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg) 1991 { 1992 struct inode *inode = file_inode(filp); 1993 1994 return put_user(inode->i_generation, (int __user *)arg); 1995 } 1996 1997 static int f2fs_ioc_start_atomic_write(struct file *filp) 1998 { 1999 struct inode *inode = file_inode(filp); 2000 struct f2fs_inode_info *fi = F2FS_I(inode); 2001 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2002 int ret; 2003 2004 if (!inode_owner_or_capable(inode)) 2005 return -EACCES; 2006 2007 if (!S_ISREG(inode->i_mode)) 2008 return -EINVAL; 2009 2010 if (filp->f_flags & O_DIRECT) 2011 return -EINVAL; 2012 2013 ret = mnt_want_write_file(filp); 2014 if (ret) 2015 return ret; 2016 2017 inode_lock(inode); 2018 2019 f2fs_disable_compressed_file(inode); 2020 2021 if (f2fs_is_atomic_file(inode)) { 2022 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) 2023 ret = -EINVAL; 2024 goto out; 2025 } 2026 2027 ret = f2fs_convert_inline_inode(inode); 2028 if (ret) 2029 goto out; 2030 2031 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 2032 2033 /* 2034 * Should wait end_io to count F2FS_WB_CP_DATA correctly by 2035 * f2fs_is_atomic_file. 2036 */ 2037 if (get_dirty_pages(inode)) 2038 f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u", 2039 inode->i_ino, get_dirty_pages(inode)); 2040 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX); 2041 if (ret) { 2042 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 2043 goto out; 2044 } 2045 2046 spin_lock(&sbi->inode_lock[ATOMIC_FILE]); 2047 if (list_empty(&fi->inmem_ilist)) 2048 list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]); 2049 sbi->atomic_files++; 2050 spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); 2051 2052 /* add inode in inmem_list first and set atomic_file */ 2053 set_inode_flag(inode, FI_ATOMIC_FILE); 2054 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST); 2055 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 2056 2057 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 2058 F2FS_I(inode)->inmem_task = current; 2059 stat_update_max_atomic_write(inode); 2060 out: 2061 inode_unlock(inode); 2062 mnt_drop_write_file(filp); 2063 return ret; 2064 } 2065 2066 static int f2fs_ioc_commit_atomic_write(struct file *filp) 2067 { 2068 struct inode *inode = file_inode(filp); 2069 int ret; 2070 2071 if (!inode_owner_or_capable(inode)) 2072 return -EACCES; 2073 2074 ret = mnt_want_write_file(filp); 2075 if (ret) 2076 return ret; 2077 2078 f2fs_balance_fs(F2FS_I_SB(inode), true); 2079 2080 inode_lock(inode); 2081 2082 if (f2fs_is_volatile_file(inode)) { 2083 ret = -EINVAL; 2084 goto err_out; 2085 } 2086 2087 if (f2fs_is_atomic_file(inode)) { 2088 ret = f2fs_commit_inmem_pages(inode); 2089 if (ret) 2090 goto err_out; 2091 2092 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true); 2093 if (!ret) 2094 f2fs_drop_inmem_pages(inode); 2095 } else { 2096 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false); 2097 } 2098 err_out: 2099 if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) { 2100 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST); 2101 ret = -EINVAL; 2102 } 2103 inode_unlock(inode); 2104 mnt_drop_write_file(filp); 2105 return ret; 2106 } 2107 2108 static int f2fs_ioc_start_volatile_write(struct file *filp) 2109 { 2110 struct inode *inode = file_inode(filp); 2111 int ret; 2112 2113 if (!inode_owner_or_capable(inode)) 2114 return -EACCES; 2115 2116 if (!S_ISREG(inode->i_mode)) 2117 return -EINVAL; 2118 2119 ret = mnt_want_write_file(filp); 2120 if (ret) 2121 return ret; 2122 2123 inode_lock(inode); 2124 2125 if (f2fs_is_volatile_file(inode)) 2126 goto out; 2127 2128 ret = f2fs_convert_inline_inode(inode); 2129 if (ret) 2130 goto out; 2131 2132 stat_inc_volatile_write(inode); 2133 stat_update_max_volatile_write(inode); 2134 2135 set_inode_flag(inode, FI_VOLATILE_FILE); 2136 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 2137 out: 2138 inode_unlock(inode); 2139 mnt_drop_write_file(filp); 2140 return ret; 2141 } 2142 2143 static int f2fs_ioc_release_volatile_write(struct file *filp) 2144 { 2145 struct inode *inode = file_inode(filp); 2146 int ret; 2147 2148 if (!inode_owner_or_capable(inode)) 2149 return -EACCES; 2150 2151 ret = mnt_want_write_file(filp); 2152 if (ret) 2153 return ret; 2154 2155 inode_lock(inode); 2156 2157 if (!f2fs_is_volatile_file(inode)) 2158 goto out; 2159 2160 if (!f2fs_is_first_block_written(inode)) { 2161 ret = truncate_partial_data_page(inode, 0, true); 2162 goto out; 2163 } 2164 2165 ret = punch_hole(inode, 0, F2FS_BLKSIZE); 2166 out: 2167 inode_unlock(inode); 2168 mnt_drop_write_file(filp); 2169 return ret; 2170 } 2171 2172 static int f2fs_ioc_abort_volatile_write(struct file *filp) 2173 { 2174 struct inode *inode = file_inode(filp); 2175 int ret; 2176 2177 if (!inode_owner_or_capable(inode)) 2178 return -EACCES; 2179 2180 ret = mnt_want_write_file(filp); 2181 if (ret) 2182 return ret; 2183 2184 inode_lock(inode); 2185 2186 if (f2fs_is_atomic_file(inode)) 2187 f2fs_drop_inmem_pages(inode); 2188 if (f2fs_is_volatile_file(inode)) { 2189 clear_inode_flag(inode, FI_VOLATILE_FILE); 2190 stat_dec_volatile_write(inode); 2191 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true); 2192 } 2193 2194 clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST); 2195 2196 inode_unlock(inode); 2197 2198 mnt_drop_write_file(filp); 2199 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 2200 return ret; 2201 } 2202 2203 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg) 2204 { 2205 struct inode *inode = file_inode(filp); 2206 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2207 struct super_block *sb = sbi->sb; 2208 __u32 in; 2209 int ret = 0; 2210 2211 if (!capable(CAP_SYS_ADMIN)) 2212 return -EPERM; 2213 2214 if (get_user(in, (__u32 __user *)arg)) 2215 return -EFAULT; 2216 2217 if (in != F2FS_GOING_DOWN_FULLSYNC) { 2218 ret = mnt_want_write_file(filp); 2219 if (ret) 2220 return ret; 2221 } 2222 2223 switch (in) { 2224 case F2FS_GOING_DOWN_FULLSYNC: 2225 sb = freeze_bdev(sb->s_bdev); 2226 if (IS_ERR(sb)) { 2227 ret = PTR_ERR(sb); 2228 goto out; 2229 } 2230 if (sb) { 2231 f2fs_stop_checkpoint(sbi, false); 2232 set_sbi_flag(sbi, SBI_IS_SHUTDOWN); 2233 thaw_bdev(sb->s_bdev, sb); 2234 } 2235 break; 2236 case F2FS_GOING_DOWN_METASYNC: 2237 /* do checkpoint only */ 2238 ret = f2fs_sync_fs(sb, 1); 2239 if (ret) 2240 goto out; 2241 f2fs_stop_checkpoint(sbi, false); 2242 set_sbi_flag(sbi, SBI_IS_SHUTDOWN); 2243 break; 2244 case F2FS_GOING_DOWN_NOSYNC: 2245 f2fs_stop_checkpoint(sbi, false); 2246 set_sbi_flag(sbi, SBI_IS_SHUTDOWN); 2247 break; 2248 case F2FS_GOING_DOWN_METAFLUSH: 2249 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO); 2250 f2fs_stop_checkpoint(sbi, false); 2251 set_sbi_flag(sbi, SBI_IS_SHUTDOWN); 2252 break; 2253 case F2FS_GOING_DOWN_NEED_FSCK: 2254 set_sbi_flag(sbi, SBI_NEED_FSCK); 2255 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK); 2256 set_sbi_flag(sbi, SBI_IS_DIRTY); 2257 /* do checkpoint only */ 2258 ret = f2fs_sync_fs(sb, 1); 2259 goto out; 2260 default: 2261 ret = -EINVAL; 2262 goto out; 2263 } 2264 2265 f2fs_stop_gc_thread(sbi); 2266 f2fs_stop_discard_thread(sbi); 2267 2268 f2fs_drop_discard_cmd(sbi); 2269 clear_opt(sbi, DISCARD); 2270 2271 f2fs_update_time(sbi, REQ_TIME); 2272 out: 2273 if (in != F2FS_GOING_DOWN_FULLSYNC) 2274 mnt_drop_write_file(filp); 2275 2276 trace_f2fs_shutdown(sbi, in, ret); 2277 2278 return ret; 2279 } 2280 2281 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg) 2282 { 2283 struct inode *inode = file_inode(filp); 2284 struct super_block *sb = inode->i_sb; 2285 struct request_queue *q = bdev_get_queue(sb->s_bdev); 2286 struct fstrim_range range; 2287 int ret; 2288 2289 if (!capable(CAP_SYS_ADMIN)) 2290 return -EPERM; 2291 2292 if (!f2fs_hw_support_discard(F2FS_SB(sb))) 2293 return -EOPNOTSUPP; 2294 2295 if (copy_from_user(&range, (struct fstrim_range __user *)arg, 2296 sizeof(range))) 2297 return -EFAULT; 2298 2299 ret = mnt_want_write_file(filp); 2300 if (ret) 2301 return ret; 2302 2303 range.minlen = max((unsigned int)range.minlen, 2304 q->limits.discard_granularity); 2305 ret = f2fs_trim_fs(F2FS_SB(sb), &range); 2306 mnt_drop_write_file(filp); 2307 if (ret < 0) 2308 return ret; 2309 2310 if (copy_to_user((struct fstrim_range __user *)arg, &range, 2311 sizeof(range))) 2312 return -EFAULT; 2313 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 2314 return 0; 2315 } 2316 2317 static bool uuid_is_nonzero(__u8 u[16]) 2318 { 2319 int i; 2320 2321 for (i = 0; i < 16; i++) 2322 if (u[i]) 2323 return true; 2324 return false; 2325 } 2326 2327 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg) 2328 { 2329 struct inode *inode = file_inode(filp); 2330 2331 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode))) 2332 return -EOPNOTSUPP; 2333 2334 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 2335 2336 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); 2337 } 2338 2339 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg) 2340 { 2341 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) 2342 return -EOPNOTSUPP; 2343 return fscrypt_ioctl_get_policy(filp, (void __user *)arg); 2344 } 2345 2346 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg) 2347 { 2348 struct inode *inode = file_inode(filp); 2349 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2350 int err; 2351 2352 if (!f2fs_sb_has_encrypt(sbi)) 2353 return -EOPNOTSUPP; 2354 2355 err = mnt_want_write_file(filp); 2356 if (err) 2357 return err; 2358 2359 down_write(&sbi->sb_lock); 2360 2361 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt)) 2362 goto got_it; 2363 2364 /* update superblock with uuid */ 2365 generate_random_uuid(sbi->raw_super->encrypt_pw_salt); 2366 2367 err = f2fs_commit_super(sbi, false); 2368 if (err) { 2369 /* undo new data */ 2370 memset(sbi->raw_super->encrypt_pw_salt, 0, 16); 2371 goto out_err; 2372 } 2373 got_it: 2374 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt, 2375 16)) 2376 err = -EFAULT; 2377 out_err: 2378 up_write(&sbi->sb_lock); 2379 mnt_drop_write_file(filp); 2380 return err; 2381 } 2382 2383 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp, 2384 unsigned long arg) 2385 { 2386 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) 2387 return -EOPNOTSUPP; 2388 2389 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg); 2390 } 2391 2392 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg) 2393 { 2394 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) 2395 return -EOPNOTSUPP; 2396 2397 return fscrypt_ioctl_add_key(filp, (void __user *)arg); 2398 } 2399 2400 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg) 2401 { 2402 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) 2403 return -EOPNOTSUPP; 2404 2405 return fscrypt_ioctl_remove_key(filp, (void __user *)arg); 2406 } 2407 2408 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp, 2409 unsigned long arg) 2410 { 2411 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) 2412 return -EOPNOTSUPP; 2413 2414 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg); 2415 } 2416 2417 static int f2fs_ioc_get_encryption_key_status(struct file *filp, 2418 unsigned long arg) 2419 { 2420 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) 2421 return -EOPNOTSUPP; 2422 2423 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg); 2424 } 2425 2426 static int f2fs_ioc_gc(struct file *filp, unsigned long arg) 2427 { 2428 struct inode *inode = file_inode(filp); 2429 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2430 __u32 sync; 2431 int ret; 2432 2433 if (!capable(CAP_SYS_ADMIN)) 2434 return -EPERM; 2435 2436 if (get_user(sync, (__u32 __user *)arg)) 2437 return -EFAULT; 2438 2439 if (f2fs_readonly(sbi->sb)) 2440 return -EROFS; 2441 2442 ret = mnt_want_write_file(filp); 2443 if (ret) 2444 return ret; 2445 2446 if (!sync) { 2447 if (!down_write_trylock(&sbi->gc_lock)) { 2448 ret = -EBUSY; 2449 goto out; 2450 } 2451 } else { 2452 down_write(&sbi->gc_lock); 2453 } 2454 2455 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO); 2456 out: 2457 mnt_drop_write_file(filp); 2458 return ret; 2459 } 2460 2461 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg) 2462 { 2463 struct inode *inode = file_inode(filp); 2464 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2465 struct f2fs_gc_range range; 2466 u64 end; 2467 int ret; 2468 2469 if (!capable(CAP_SYS_ADMIN)) 2470 return -EPERM; 2471 2472 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg, 2473 sizeof(range))) 2474 return -EFAULT; 2475 2476 if (f2fs_readonly(sbi->sb)) 2477 return -EROFS; 2478 2479 end = range.start + range.len; 2480 if (end < range.start || range.start < MAIN_BLKADDR(sbi) || 2481 end >= MAX_BLKADDR(sbi)) 2482 return -EINVAL; 2483 2484 ret = mnt_want_write_file(filp); 2485 if (ret) 2486 return ret; 2487 2488 do_more: 2489 if (!range.sync) { 2490 if (!down_write_trylock(&sbi->gc_lock)) { 2491 ret = -EBUSY; 2492 goto out; 2493 } 2494 } else { 2495 down_write(&sbi->gc_lock); 2496 } 2497 2498 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start)); 2499 range.start += BLKS_PER_SEC(sbi); 2500 if (range.start <= end) 2501 goto do_more; 2502 out: 2503 mnt_drop_write_file(filp); 2504 return ret; 2505 } 2506 2507 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg) 2508 { 2509 struct inode *inode = file_inode(filp); 2510 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2511 int ret; 2512 2513 if (!capable(CAP_SYS_ADMIN)) 2514 return -EPERM; 2515 2516 if (f2fs_readonly(sbi->sb)) 2517 return -EROFS; 2518 2519 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 2520 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled."); 2521 return -EINVAL; 2522 } 2523 2524 ret = mnt_want_write_file(filp); 2525 if (ret) 2526 return ret; 2527 2528 ret = f2fs_sync_fs(sbi->sb, 1); 2529 2530 mnt_drop_write_file(filp); 2531 return ret; 2532 } 2533 2534 static int f2fs_defragment_range(struct f2fs_sb_info *sbi, 2535 struct file *filp, 2536 struct f2fs_defragment *range) 2537 { 2538 struct inode *inode = file_inode(filp); 2539 struct f2fs_map_blocks map = { .m_next_extent = NULL, 2540 .m_seg_type = NO_CHECK_TYPE , 2541 .m_may_create = false }; 2542 struct extent_info ei = {0, 0, 0}; 2543 pgoff_t pg_start, pg_end, next_pgofs; 2544 unsigned int blk_per_seg = sbi->blocks_per_seg; 2545 unsigned int total = 0, sec_num; 2546 block_t blk_end = 0; 2547 bool fragmented = false; 2548 int err; 2549 2550 /* if in-place-update policy is enabled, don't waste time here */ 2551 if (f2fs_should_update_inplace(inode, NULL)) 2552 return -EINVAL; 2553 2554 pg_start = range->start >> PAGE_SHIFT; 2555 pg_end = (range->start + range->len) >> PAGE_SHIFT; 2556 2557 f2fs_balance_fs(sbi, true); 2558 2559 inode_lock(inode); 2560 2561 /* writeback all dirty pages in the range */ 2562 err = filemap_write_and_wait_range(inode->i_mapping, range->start, 2563 range->start + range->len - 1); 2564 if (err) 2565 goto out; 2566 2567 /* 2568 * lookup mapping info in extent cache, skip defragmenting if physical 2569 * block addresses are continuous. 2570 */ 2571 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) { 2572 if (ei.fofs + ei.len >= pg_end) 2573 goto out; 2574 } 2575 2576 map.m_lblk = pg_start; 2577 map.m_next_pgofs = &next_pgofs; 2578 2579 /* 2580 * lookup mapping info in dnode page cache, skip defragmenting if all 2581 * physical block addresses are continuous even if there are hole(s) 2582 * in logical blocks. 2583 */ 2584 while (map.m_lblk < pg_end) { 2585 map.m_len = pg_end - map.m_lblk; 2586 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT); 2587 if (err) 2588 goto out; 2589 2590 if (!(map.m_flags & F2FS_MAP_FLAGS)) { 2591 map.m_lblk = next_pgofs; 2592 continue; 2593 } 2594 2595 if (blk_end && blk_end != map.m_pblk) 2596 fragmented = true; 2597 2598 /* record total count of block that we're going to move */ 2599 total += map.m_len; 2600 2601 blk_end = map.m_pblk + map.m_len; 2602 2603 map.m_lblk += map.m_len; 2604 } 2605 2606 if (!fragmented) { 2607 total = 0; 2608 goto out; 2609 } 2610 2611 sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi)); 2612 2613 /* 2614 * make sure there are enough free section for LFS allocation, this can 2615 * avoid defragment running in SSR mode when free section are allocated 2616 * intensively 2617 */ 2618 if (has_not_enough_free_secs(sbi, 0, sec_num)) { 2619 err = -EAGAIN; 2620 goto out; 2621 } 2622 2623 map.m_lblk = pg_start; 2624 map.m_len = pg_end - pg_start; 2625 total = 0; 2626 2627 while (map.m_lblk < pg_end) { 2628 pgoff_t idx; 2629 int cnt = 0; 2630 2631 do_map: 2632 map.m_len = pg_end - map.m_lblk; 2633 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT); 2634 if (err) 2635 goto clear_out; 2636 2637 if (!(map.m_flags & F2FS_MAP_FLAGS)) { 2638 map.m_lblk = next_pgofs; 2639 goto check; 2640 } 2641 2642 set_inode_flag(inode, FI_DO_DEFRAG); 2643 2644 idx = map.m_lblk; 2645 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) { 2646 struct page *page; 2647 2648 page = f2fs_get_lock_data_page(inode, idx, true); 2649 if (IS_ERR(page)) { 2650 err = PTR_ERR(page); 2651 goto clear_out; 2652 } 2653 2654 set_page_dirty(page); 2655 f2fs_put_page(page, 1); 2656 2657 idx++; 2658 cnt++; 2659 total++; 2660 } 2661 2662 map.m_lblk = idx; 2663 check: 2664 if (map.m_lblk < pg_end && cnt < blk_per_seg) 2665 goto do_map; 2666 2667 clear_inode_flag(inode, FI_DO_DEFRAG); 2668 2669 err = filemap_fdatawrite(inode->i_mapping); 2670 if (err) 2671 goto out; 2672 } 2673 clear_out: 2674 clear_inode_flag(inode, FI_DO_DEFRAG); 2675 out: 2676 inode_unlock(inode); 2677 if (!err) 2678 range->len = (u64)total << PAGE_SHIFT; 2679 return err; 2680 } 2681 2682 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg) 2683 { 2684 struct inode *inode = file_inode(filp); 2685 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2686 struct f2fs_defragment range; 2687 int err; 2688 2689 if (!capable(CAP_SYS_ADMIN)) 2690 return -EPERM; 2691 2692 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode)) 2693 return -EINVAL; 2694 2695 if (f2fs_readonly(sbi->sb)) 2696 return -EROFS; 2697 2698 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg, 2699 sizeof(range))) 2700 return -EFAULT; 2701 2702 /* verify alignment of offset & size */ 2703 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1)) 2704 return -EINVAL; 2705 2706 if (unlikely((range.start + range.len) >> PAGE_SHIFT > 2707 sbi->max_file_blocks)) 2708 return -EINVAL; 2709 2710 err = mnt_want_write_file(filp); 2711 if (err) 2712 return err; 2713 2714 err = f2fs_defragment_range(sbi, filp, &range); 2715 mnt_drop_write_file(filp); 2716 2717 f2fs_update_time(sbi, REQ_TIME); 2718 if (err < 0) 2719 return err; 2720 2721 if (copy_to_user((struct f2fs_defragment __user *)arg, &range, 2722 sizeof(range))) 2723 return -EFAULT; 2724 2725 return 0; 2726 } 2727 2728 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in, 2729 struct file *file_out, loff_t pos_out, size_t len) 2730 { 2731 struct inode *src = file_inode(file_in); 2732 struct inode *dst = file_inode(file_out); 2733 struct f2fs_sb_info *sbi = F2FS_I_SB(src); 2734 size_t olen = len, dst_max_i_size = 0; 2735 size_t dst_osize; 2736 int ret; 2737 2738 if (file_in->f_path.mnt != file_out->f_path.mnt || 2739 src->i_sb != dst->i_sb) 2740 return -EXDEV; 2741 2742 if (unlikely(f2fs_readonly(src->i_sb))) 2743 return -EROFS; 2744 2745 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode)) 2746 return -EINVAL; 2747 2748 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst)) 2749 return -EOPNOTSUPP; 2750 2751 if (src == dst) { 2752 if (pos_in == pos_out) 2753 return 0; 2754 if (pos_out > pos_in && pos_out < pos_in + len) 2755 return -EINVAL; 2756 } 2757 2758 inode_lock(src); 2759 if (src != dst) { 2760 ret = -EBUSY; 2761 if (!inode_trylock(dst)) 2762 goto out; 2763 } 2764 2765 ret = -EINVAL; 2766 if (pos_in + len > src->i_size || pos_in + len < pos_in) 2767 goto out_unlock; 2768 if (len == 0) 2769 olen = len = src->i_size - pos_in; 2770 if (pos_in + len == src->i_size) 2771 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in; 2772 if (len == 0) { 2773 ret = 0; 2774 goto out_unlock; 2775 } 2776 2777 dst_osize = dst->i_size; 2778 if (pos_out + olen > dst->i_size) 2779 dst_max_i_size = pos_out + olen; 2780 2781 /* verify the end result is block aligned */ 2782 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) || 2783 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) || 2784 !IS_ALIGNED(pos_out, F2FS_BLKSIZE)) 2785 goto out_unlock; 2786 2787 ret = f2fs_convert_inline_inode(src); 2788 if (ret) 2789 goto out_unlock; 2790 2791 ret = f2fs_convert_inline_inode(dst); 2792 if (ret) 2793 goto out_unlock; 2794 2795 /* write out all dirty pages from offset */ 2796 ret = filemap_write_and_wait_range(src->i_mapping, 2797 pos_in, pos_in + len); 2798 if (ret) 2799 goto out_unlock; 2800 2801 ret = filemap_write_and_wait_range(dst->i_mapping, 2802 pos_out, pos_out + len); 2803 if (ret) 2804 goto out_unlock; 2805 2806 f2fs_balance_fs(sbi, true); 2807 2808 down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]); 2809 if (src != dst) { 2810 ret = -EBUSY; 2811 if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE])) 2812 goto out_src; 2813 } 2814 2815 f2fs_lock_op(sbi); 2816 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS, 2817 pos_out >> F2FS_BLKSIZE_BITS, 2818 len >> F2FS_BLKSIZE_BITS, false); 2819 2820 if (!ret) { 2821 if (dst_max_i_size) 2822 f2fs_i_size_write(dst, dst_max_i_size); 2823 else if (dst_osize != dst->i_size) 2824 f2fs_i_size_write(dst, dst_osize); 2825 } 2826 f2fs_unlock_op(sbi); 2827 2828 if (src != dst) 2829 up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]); 2830 out_src: 2831 up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]); 2832 out_unlock: 2833 if (src != dst) 2834 inode_unlock(dst); 2835 out: 2836 inode_unlock(src); 2837 return ret; 2838 } 2839 2840 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg) 2841 { 2842 struct f2fs_move_range range; 2843 struct fd dst; 2844 int err; 2845 2846 if (!(filp->f_mode & FMODE_READ) || 2847 !(filp->f_mode & FMODE_WRITE)) 2848 return -EBADF; 2849 2850 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg, 2851 sizeof(range))) 2852 return -EFAULT; 2853 2854 dst = fdget(range.dst_fd); 2855 if (!dst.file) 2856 return -EBADF; 2857 2858 if (!(dst.file->f_mode & FMODE_WRITE)) { 2859 err = -EBADF; 2860 goto err_out; 2861 } 2862 2863 err = mnt_want_write_file(filp); 2864 if (err) 2865 goto err_out; 2866 2867 err = f2fs_move_file_range(filp, range.pos_in, dst.file, 2868 range.pos_out, range.len); 2869 2870 mnt_drop_write_file(filp); 2871 if (err) 2872 goto err_out; 2873 2874 if (copy_to_user((struct f2fs_move_range __user *)arg, 2875 &range, sizeof(range))) 2876 err = -EFAULT; 2877 err_out: 2878 fdput(dst); 2879 return err; 2880 } 2881 2882 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg) 2883 { 2884 struct inode *inode = file_inode(filp); 2885 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2886 struct sit_info *sm = SIT_I(sbi); 2887 unsigned int start_segno = 0, end_segno = 0; 2888 unsigned int dev_start_segno = 0, dev_end_segno = 0; 2889 struct f2fs_flush_device range; 2890 int ret; 2891 2892 if (!capable(CAP_SYS_ADMIN)) 2893 return -EPERM; 2894 2895 if (f2fs_readonly(sbi->sb)) 2896 return -EROFS; 2897 2898 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) 2899 return -EINVAL; 2900 2901 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg, 2902 sizeof(range))) 2903 return -EFAULT; 2904 2905 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num || 2906 __is_large_section(sbi)) { 2907 f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1", 2908 range.dev_num, sbi->s_ndevs, sbi->segs_per_sec); 2909 return -EINVAL; 2910 } 2911 2912 ret = mnt_want_write_file(filp); 2913 if (ret) 2914 return ret; 2915 2916 if (range.dev_num != 0) 2917 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk); 2918 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk); 2919 2920 start_segno = sm->last_victim[FLUSH_DEVICE]; 2921 if (start_segno < dev_start_segno || start_segno >= dev_end_segno) 2922 start_segno = dev_start_segno; 2923 end_segno = min(start_segno + range.segments, dev_end_segno); 2924 2925 while (start_segno < end_segno) { 2926 if (!down_write_trylock(&sbi->gc_lock)) { 2927 ret = -EBUSY; 2928 goto out; 2929 } 2930 sm->last_victim[GC_CB] = end_segno + 1; 2931 sm->last_victim[GC_GREEDY] = end_segno + 1; 2932 sm->last_victim[ALLOC_NEXT] = end_segno + 1; 2933 ret = f2fs_gc(sbi, true, true, start_segno); 2934 if (ret == -EAGAIN) 2935 ret = 0; 2936 else if (ret < 0) 2937 break; 2938 start_segno++; 2939 } 2940 out: 2941 mnt_drop_write_file(filp); 2942 return ret; 2943 } 2944 2945 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg) 2946 { 2947 struct inode *inode = file_inode(filp); 2948 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature); 2949 2950 /* Must validate to set it with SQLite behavior in Android. */ 2951 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE; 2952 2953 return put_user(sb_feature, (u32 __user *)arg); 2954 } 2955 2956 #ifdef CONFIG_QUOTA 2957 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid) 2958 { 2959 struct dquot *transfer_to[MAXQUOTAS] = {}; 2960 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2961 struct super_block *sb = sbi->sb; 2962 int err = 0; 2963 2964 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid)); 2965 if (!IS_ERR(transfer_to[PRJQUOTA])) { 2966 err = __dquot_transfer(inode, transfer_to); 2967 if (err) 2968 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 2969 dqput(transfer_to[PRJQUOTA]); 2970 } 2971 return err; 2972 } 2973 2974 static int f2fs_ioc_setproject(struct file *filp, __u32 projid) 2975 { 2976 struct inode *inode = file_inode(filp); 2977 struct f2fs_inode_info *fi = F2FS_I(inode); 2978 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2979 struct page *ipage; 2980 kprojid_t kprojid; 2981 int err; 2982 2983 if (!f2fs_sb_has_project_quota(sbi)) { 2984 if (projid != F2FS_DEF_PROJID) 2985 return -EOPNOTSUPP; 2986 else 2987 return 0; 2988 } 2989 2990 if (!f2fs_has_extra_attr(inode)) 2991 return -EOPNOTSUPP; 2992 2993 kprojid = make_kprojid(&init_user_ns, (projid_t)projid); 2994 2995 if (projid_eq(kprojid, F2FS_I(inode)->i_projid)) 2996 return 0; 2997 2998 err = -EPERM; 2999 /* Is it quota file? Do not allow user to mess with it */ 3000 if (IS_NOQUOTA(inode)) 3001 return err; 3002 3003 ipage = f2fs_get_node_page(sbi, inode->i_ino); 3004 if (IS_ERR(ipage)) 3005 return PTR_ERR(ipage); 3006 3007 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize, 3008 i_projid)) { 3009 err = -EOVERFLOW; 3010 f2fs_put_page(ipage, 1); 3011 return err; 3012 } 3013 f2fs_put_page(ipage, 1); 3014 3015 err = dquot_initialize(inode); 3016 if (err) 3017 return err; 3018 3019 f2fs_lock_op(sbi); 3020 err = f2fs_transfer_project_quota(inode, kprojid); 3021 if (err) 3022 goto out_unlock; 3023 3024 F2FS_I(inode)->i_projid = kprojid; 3025 inode->i_ctime = current_time(inode); 3026 f2fs_mark_inode_dirty_sync(inode, true); 3027 out_unlock: 3028 f2fs_unlock_op(sbi); 3029 return err; 3030 } 3031 #else 3032 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid) 3033 { 3034 return 0; 3035 } 3036 3037 static int f2fs_ioc_setproject(struct file *filp, __u32 projid) 3038 { 3039 if (projid != F2FS_DEF_PROJID) 3040 return -EOPNOTSUPP; 3041 return 0; 3042 } 3043 #endif 3044 3045 /* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */ 3046 3047 /* 3048 * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable 3049 * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its 3050 * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS. 3051 */ 3052 3053 static const struct { 3054 u32 iflag; 3055 u32 xflag; 3056 } f2fs_xflags_map[] = { 3057 { F2FS_SYNC_FL, FS_XFLAG_SYNC }, 3058 { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE }, 3059 { F2FS_APPEND_FL, FS_XFLAG_APPEND }, 3060 { F2FS_NODUMP_FL, FS_XFLAG_NODUMP }, 3061 { F2FS_NOATIME_FL, FS_XFLAG_NOATIME }, 3062 { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT }, 3063 }; 3064 3065 #define F2FS_SUPPORTED_XFLAGS ( \ 3066 FS_XFLAG_SYNC | \ 3067 FS_XFLAG_IMMUTABLE | \ 3068 FS_XFLAG_APPEND | \ 3069 FS_XFLAG_NODUMP | \ 3070 FS_XFLAG_NOATIME | \ 3071 FS_XFLAG_PROJINHERIT) 3072 3073 /* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */ 3074 static inline u32 f2fs_iflags_to_xflags(u32 iflags) 3075 { 3076 u32 xflags = 0; 3077 int i; 3078 3079 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++) 3080 if (iflags & f2fs_xflags_map[i].iflag) 3081 xflags |= f2fs_xflags_map[i].xflag; 3082 3083 return xflags; 3084 } 3085 3086 /* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */ 3087 static inline u32 f2fs_xflags_to_iflags(u32 xflags) 3088 { 3089 u32 iflags = 0; 3090 int i; 3091 3092 for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++) 3093 if (xflags & f2fs_xflags_map[i].xflag) 3094 iflags |= f2fs_xflags_map[i].iflag; 3095 3096 return iflags; 3097 } 3098 3099 static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa) 3100 { 3101 struct f2fs_inode_info *fi = F2FS_I(inode); 3102 3103 simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags)); 3104 3105 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode))) 3106 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid); 3107 } 3108 3109 static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg) 3110 { 3111 struct inode *inode = file_inode(filp); 3112 struct fsxattr fa; 3113 3114 f2fs_fill_fsxattr(inode, &fa); 3115 3116 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa))) 3117 return -EFAULT; 3118 return 0; 3119 } 3120 3121 static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg) 3122 { 3123 struct inode *inode = file_inode(filp); 3124 struct fsxattr fa, old_fa; 3125 u32 iflags; 3126 int err; 3127 3128 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa))) 3129 return -EFAULT; 3130 3131 /* Make sure caller has proper permission */ 3132 if (!inode_owner_or_capable(inode)) 3133 return -EACCES; 3134 3135 if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS) 3136 return -EOPNOTSUPP; 3137 3138 iflags = f2fs_xflags_to_iflags(fa.fsx_xflags); 3139 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags) 3140 return -EOPNOTSUPP; 3141 3142 err = mnt_want_write_file(filp); 3143 if (err) 3144 return err; 3145 3146 inode_lock(inode); 3147 3148 f2fs_fill_fsxattr(inode, &old_fa); 3149 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa); 3150 if (err) 3151 goto out; 3152 3153 err = f2fs_setflags_common(inode, iflags, 3154 f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS)); 3155 if (err) 3156 goto out; 3157 3158 err = f2fs_ioc_setproject(filp, fa.fsx_projid); 3159 out: 3160 inode_unlock(inode); 3161 mnt_drop_write_file(filp); 3162 return err; 3163 } 3164 3165 int f2fs_pin_file_control(struct inode *inode, bool inc) 3166 { 3167 struct f2fs_inode_info *fi = F2FS_I(inode); 3168 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3169 3170 /* Use i_gc_failures for normal file as a risk signal. */ 3171 if (inc) 3172 f2fs_i_gc_failures_write(inode, 3173 fi->i_gc_failures[GC_FAILURE_PIN] + 1); 3174 3175 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) { 3176 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials", 3177 __func__, inode->i_ino, 3178 fi->i_gc_failures[GC_FAILURE_PIN]); 3179 clear_inode_flag(inode, FI_PIN_FILE); 3180 return -EAGAIN; 3181 } 3182 return 0; 3183 } 3184 3185 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg) 3186 { 3187 struct inode *inode = file_inode(filp); 3188 __u32 pin; 3189 int ret = 0; 3190 3191 if (get_user(pin, (__u32 __user *)arg)) 3192 return -EFAULT; 3193 3194 if (!S_ISREG(inode->i_mode)) 3195 return -EINVAL; 3196 3197 if (f2fs_readonly(F2FS_I_SB(inode)->sb)) 3198 return -EROFS; 3199 3200 ret = mnt_want_write_file(filp); 3201 if (ret) 3202 return ret; 3203 3204 inode_lock(inode); 3205 3206 if (f2fs_should_update_outplace(inode, NULL)) { 3207 ret = -EINVAL; 3208 goto out; 3209 } 3210 3211 if (!pin) { 3212 clear_inode_flag(inode, FI_PIN_FILE); 3213 f2fs_i_gc_failures_write(inode, 0); 3214 goto done; 3215 } 3216 3217 if (f2fs_pin_file_control(inode, false)) { 3218 ret = -EAGAIN; 3219 goto out; 3220 } 3221 3222 ret = f2fs_convert_inline_inode(inode); 3223 if (ret) 3224 goto out; 3225 3226 if (f2fs_disable_compressed_file(inode)) { 3227 ret = -EOPNOTSUPP; 3228 goto out; 3229 } 3230 3231 set_inode_flag(inode, FI_PIN_FILE); 3232 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]; 3233 done: 3234 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 3235 out: 3236 inode_unlock(inode); 3237 mnt_drop_write_file(filp); 3238 return ret; 3239 } 3240 3241 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg) 3242 { 3243 struct inode *inode = file_inode(filp); 3244 __u32 pin = 0; 3245 3246 if (is_inode_flag_set(inode, FI_PIN_FILE)) 3247 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]; 3248 return put_user(pin, (u32 __user *)arg); 3249 } 3250 3251 int f2fs_precache_extents(struct inode *inode) 3252 { 3253 struct f2fs_inode_info *fi = F2FS_I(inode); 3254 struct f2fs_map_blocks map; 3255 pgoff_t m_next_extent; 3256 loff_t end; 3257 int err; 3258 3259 if (is_inode_flag_set(inode, FI_NO_EXTENT)) 3260 return -EOPNOTSUPP; 3261 3262 map.m_lblk = 0; 3263 map.m_next_pgofs = NULL; 3264 map.m_next_extent = &m_next_extent; 3265 map.m_seg_type = NO_CHECK_TYPE; 3266 map.m_may_create = false; 3267 end = F2FS_I_SB(inode)->max_file_blocks; 3268 3269 while (map.m_lblk < end) { 3270 map.m_len = end - map.m_lblk; 3271 3272 down_write(&fi->i_gc_rwsem[WRITE]); 3273 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE); 3274 up_write(&fi->i_gc_rwsem[WRITE]); 3275 if (err) 3276 return err; 3277 3278 map.m_lblk = m_next_extent; 3279 } 3280 3281 return err; 3282 } 3283 3284 static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg) 3285 { 3286 return f2fs_precache_extents(file_inode(filp)); 3287 } 3288 3289 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg) 3290 { 3291 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp)); 3292 __u64 block_count; 3293 int ret; 3294 3295 if (!capable(CAP_SYS_ADMIN)) 3296 return -EPERM; 3297 3298 if (f2fs_readonly(sbi->sb)) 3299 return -EROFS; 3300 3301 if (copy_from_user(&block_count, (void __user *)arg, 3302 sizeof(block_count))) 3303 return -EFAULT; 3304 3305 ret = f2fs_resize_fs(sbi, block_count); 3306 3307 return ret; 3308 } 3309 3310 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg) 3311 { 3312 struct inode *inode = file_inode(filp); 3313 3314 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 3315 3316 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) { 3317 f2fs_warn(F2FS_I_SB(inode), 3318 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n", 3319 inode->i_ino); 3320 return -EOPNOTSUPP; 3321 } 3322 3323 return fsverity_ioctl_enable(filp, (const void __user *)arg); 3324 } 3325 3326 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg) 3327 { 3328 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp)))) 3329 return -EOPNOTSUPP; 3330 3331 return fsverity_ioctl_measure(filp, (void __user *)arg); 3332 } 3333 3334 static int f2fs_get_volume_name(struct file *filp, unsigned long arg) 3335 { 3336 struct inode *inode = file_inode(filp); 3337 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3338 char *vbuf; 3339 int count; 3340 int err = 0; 3341 3342 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL); 3343 if (!vbuf) 3344 return -ENOMEM; 3345 3346 down_read(&sbi->sb_lock); 3347 count = utf16s_to_utf8s(sbi->raw_super->volume_name, 3348 ARRAY_SIZE(sbi->raw_super->volume_name), 3349 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME); 3350 up_read(&sbi->sb_lock); 3351 3352 if (copy_to_user((char __user *)arg, vbuf, 3353 min(FSLABEL_MAX, count))) 3354 err = -EFAULT; 3355 3356 kvfree(vbuf); 3357 return err; 3358 } 3359 3360 static int f2fs_set_volume_name(struct file *filp, unsigned long arg) 3361 { 3362 struct inode *inode = file_inode(filp); 3363 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3364 char *vbuf; 3365 int err = 0; 3366 3367 if (!capable(CAP_SYS_ADMIN)) 3368 return -EPERM; 3369 3370 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX); 3371 if (IS_ERR(vbuf)) 3372 return PTR_ERR(vbuf); 3373 3374 err = mnt_want_write_file(filp); 3375 if (err) 3376 goto out; 3377 3378 down_write(&sbi->sb_lock); 3379 3380 memset(sbi->raw_super->volume_name, 0, 3381 sizeof(sbi->raw_super->volume_name)); 3382 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN, 3383 sbi->raw_super->volume_name, 3384 ARRAY_SIZE(sbi->raw_super->volume_name)); 3385 3386 err = f2fs_commit_super(sbi, false); 3387 3388 up_write(&sbi->sb_lock); 3389 3390 mnt_drop_write_file(filp); 3391 out: 3392 kfree(vbuf); 3393 return err; 3394 } 3395 3396 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 3397 { 3398 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp))))) 3399 return -EIO; 3400 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp)))) 3401 return -ENOSPC; 3402 3403 switch (cmd) { 3404 case F2FS_IOC_GETFLAGS: 3405 return f2fs_ioc_getflags(filp, arg); 3406 case F2FS_IOC_SETFLAGS: 3407 return f2fs_ioc_setflags(filp, arg); 3408 case F2FS_IOC_GETVERSION: 3409 return f2fs_ioc_getversion(filp, arg); 3410 case F2FS_IOC_START_ATOMIC_WRITE: 3411 return f2fs_ioc_start_atomic_write(filp); 3412 case F2FS_IOC_COMMIT_ATOMIC_WRITE: 3413 return f2fs_ioc_commit_atomic_write(filp); 3414 case F2FS_IOC_START_VOLATILE_WRITE: 3415 return f2fs_ioc_start_volatile_write(filp); 3416 case F2FS_IOC_RELEASE_VOLATILE_WRITE: 3417 return f2fs_ioc_release_volatile_write(filp); 3418 case F2FS_IOC_ABORT_VOLATILE_WRITE: 3419 return f2fs_ioc_abort_volatile_write(filp); 3420 case F2FS_IOC_SHUTDOWN: 3421 return f2fs_ioc_shutdown(filp, arg); 3422 case FITRIM: 3423 return f2fs_ioc_fitrim(filp, arg); 3424 case F2FS_IOC_SET_ENCRYPTION_POLICY: 3425 return f2fs_ioc_set_encryption_policy(filp, arg); 3426 case F2FS_IOC_GET_ENCRYPTION_POLICY: 3427 return f2fs_ioc_get_encryption_policy(filp, arg); 3428 case F2FS_IOC_GET_ENCRYPTION_PWSALT: 3429 return f2fs_ioc_get_encryption_pwsalt(filp, arg); 3430 case FS_IOC_GET_ENCRYPTION_POLICY_EX: 3431 return f2fs_ioc_get_encryption_policy_ex(filp, arg); 3432 case FS_IOC_ADD_ENCRYPTION_KEY: 3433 return f2fs_ioc_add_encryption_key(filp, arg); 3434 case FS_IOC_REMOVE_ENCRYPTION_KEY: 3435 return f2fs_ioc_remove_encryption_key(filp, arg); 3436 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: 3437 return f2fs_ioc_remove_encryption_key_all_users(filp, arg); 3438 case FS_IOC_GET_ENCRYPTION_KEY_STATUS: 3439 return f2fs_ioc_get_encryption_key_status(filp, arg); 3440 case F2FS_IOC_GARBAGE_COLLECT: 3441 return f2fs_ioc_gc(filp, arg); 3442 case F2FS_IOC_GARBAGE_COLLECT_RANGE: 3443 return f2fs_ioc_gc_range(filp, arg); 3444 case F2FS_IOC_WRITE_CHECKPOINT: 3445 return f2fs_ioc_write_checkpoint(filp, arg); 3446 case F2FS_IOC_DEFRAGMENT: 3447 return f2fs_ioc_defragment(filp, arg); 3448 case F2FS_IOC_MOVE_RANGE: 3449 return f2fs_ioc_move_range(filp, arg); 3450 case F2FS_IOC_FLUSH_DEVICE: 3451 return f2fs_ioc_flush_device(filp, arg); 3452 case F2FS_IOC_GET_FEATURES: 3453 return f2fs_ioc_get_features(filp, arg); 3454 case F2FS_IOC_FSGETXATTR: 3455 return f2fs_ioc_fsgetxattr(filp, arg); 3456 case F2FS_IOC_FSSETXATTR: 3457 return f2fs_ioc_fssetxattr(filp, arg); 3458 case F2FS_IOC_GET_PIN_FILE: 3459 return f2fs_ioc_get_pin_file(filp, arg); 3460 case F2FS_IOC_SET_PIN_FILE: 3461 return f2fs_ioc_set_pin_file(filp, arg); 3462 case F2FS_IOC_PRECACHE_EXTENTS: 3463 return f2fs_ioc_precache_extents(filp, arg); 3464 case F2FS_IOC_RESIZE_FS: 3465 return f2fs_ioc_resize_fs(filp, arg); 3466 case FS_IOC_ENABLE_VERITY: 3467 return f2fs_ioc_enable_verity(filp, arg); 3468 case FS_IOC_MEASURE_VERITY: 3469 return f2fs_ioc_measure_verity(filp, arg); 3470 case F2FS_IOC_GET_VOLUME_NAME: 3471 return f2fs_get_volume_name(filp, arg); 3472 case F2FS_IOC_SET_VOLUME_NAME: 3473 return f2fs_set_volume_name(filp, arg); 3474 default: 3475 return -ENOTTY; 3476 } 3477 } 3478 3479 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) 3480 { 3481 struct file *file = iocb->ki_filp; 3482 struct inode *inode = file_inode(file); 3483 3484 if (!f2fs_is_compress_backend_ready(inode)) 3485 return -EOPNOTSUPP; 3486 3487 return generic_file_read_iter(iocb, iter); 3488 } 3489 3490 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 3491 { 3492 struct file *file = iocb->ki_filp; 3493 struct inode *inode = file_inode(file); 3494 ssize_t ret; 3495 3496 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) { 3497 ret = -EIO; 3498 goto out; 3499 } 3500 3501 if (!f2fs_is_compress_backend_ready(inode)) 3502 return -EOPNOTSUPP; 3503 3504 if (iocb->ki_flags & IOCB_NOWAIT) { 3505 if (!inode_trylock(inode)) { 3506 ret = -EAGAIN; 3507 goto out; 3508 } 3509 } else { 3510 inode_lock(inode); 3511 } 3512 3513 ret = generic_write_checks(iocb, from); 3514 if (ret > 0) { 3515 bool preallocated = false; 3516 size_t target_size = 0; 3517 int err; 3518 3519 if (iov_iter_fault_in_readable(from, iov_iter_count(from))) 3520 set_inode_flag(inode, FI_NO_PREALLOC); 3521 3522 if ((iocb->ki_flags & IOCB_NOWAIT)) { 3523 if (!f2fs_overwrite_io(inode, iocb->ki_pos, 3524 iov_iter_count(from)) || 3525 f2fs_has_inline_data(inode) || 3526 f2fs_force_buffered_io(inode, iocb, from)) { 3527 clear_inode_flag(inode, FI_NO_PREALLOC); 3528 inode_unlock(inode); 3529 ret = -EAGAIN; 3530 goto out; 3531 } 3532 goto write; 3533 } 3534 3535 if (is_inode_flag_set(inode, FI_NO_PREALLOC)) 3536 goto write; 3537 3538 if (iocb->ki_flags & IOCB_DIRECT) { 3539 /* 3540 * Convert inline data for Direct I/O before entering 3541 * f2fs_direct_IO(). 3542 */ 3543 err = f2fs_convert_inline_inode(inode); 3544 if (err) 3545 goto out_err; 3546 /* 3547 * If force_buffere_io() is true, we have to allocate 3548 * blocks all the time, since f2fs_direct_IO will fall 3549 * back to buffered IO. 3550 */ 3551 if (!f2fs_force_buffered_io(inode, iocb, from) && 3552 allow_outplace_dio(inode, iocb, from)) 3553 goto write; 3554 } 3555 preallocated = true; 3556 target_size = iocb->ki_pos + iov_iter_count(from); 3557 3558 err = f2fs_preallocate_blocks(iocb, from); 3559 if (err) { 3560 out_err: 3561 clear_inode_flag(inode, FI_NO_PREALLOC); 3562 inode_unlock(inode); 3563 ret = err; 3564 goto out; 3565 } 3566 write: 3567 ret = __generic_file_write_iter(iocb, from); 3568 clear_inode_flag(inode, FI_NO_PREALLOC); 3569 3570 /* if we couldn't write data, we should deallocate blocks. */ 3571 if (preallocated && i_size_read(inode) < target_size) 3572 f2fs_truncate(inode); 3573 3574 if (ret > 0) 3575 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret); 3576 } 3577 inode_unlock(inode); 3578 out: 3579 trace_f2fs_file_write_iter(inode, iocb->ki_pos, 3580 iov_iter_count(from), ret); 3581 if (ret > 0) 3582 ret = generic_write_sync(iocb, ret); 3583 return ret; 3584 } 3585 3586 #ifdef CONFIG_COMPAT 3587 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 3588 { 3589 switch (cmd) { 3590 case F2FS_IOC32_GETFLAGS: 3591 cmd = F2FS_IOC_GETFLAGS; 3592 break; 3593 case F2FS_IOC32_SETFLAGS: 3594 cmd = F2FS_IOC_SETFLAGS; 3595 break; 3596 case F2FS_IOC32_GETVERSION: 3597 cmd = F2FS_IOC_GETVERSION; 3598 break; 3599 case F2FS_IOC_START_ATOMIC_WRITE: 3600 case F2FS_IOC_COMMIT_ATOMIC_WRITE: 3601 case F2FS_IOC_START_VOLATILE_WRITE: 3602 case F2FS_IOC_RELEASE_VOLATILE_WRITE: 3603 case F2FS_IOC_ABORT_VOLATILE_WRITE: 3604 case F2FS_IOC_SHUTDOWN: 3605 case FITRIM: 3606 case F2FS_IOC_SET_ENCRYPTION_POLICY: 3607 case F2FS_IOC_GET_ENCRYPTION_PWSALT: 3608 case F2FS_IOC_GET_ENCRYPTION_POLICY: 3609 case FS_IOC_GET_ENCRYPTION_POLICY_EX: 3610 case FS_IOC_ADD_ENCRYPTION_KEY: 3611 case FS_IOC_REMOVE_ENCRYPTION_KEY: 3612 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: 3613 case FS_IOC_GET_ENCRYPTION_KEY_STATUS: 3614 case F2FS_IOC_GARBAGE_COLLECT: 3615 case F2FS_IOC_GARBAGE_COLLECT_RANGE: 3616 case F2FS_IOC_WRITE_CHECKPOINT: 3617 case F2FS_IOC_DEFRAGMENT: 3618 case F2FS_IOC_MOVE_RANGE: 3619 case F2FS_IOC_FLUSH_DEVICE: 3620 case F2FS_IOC_GET_FEATURES: 3621 case F2FS_IOC_FSGETXATTR: 3622 case F2FS_IOC_FSSETXATTR: 3623 case F2FS_IOC_GET_PIN_FILE: 3624 case F2FS_IOC_SET_PIN_FILE: 3625 case F2FS_IOC_PRECACHE_EXTENTS: 3626 case F2FS_IOC_RESIZE_FS: 3627 case FS_IOC_ENABLE_VERITY: 3628 case FS_IOC_MEASURE_VERITY: 3629 case F2FS_IOC_GET_VOLUME_NAME: 3630 case F2FS_IOC_SET_VOLUME_NAME: 3631 break; 3632 default: 3633 return -ENOIOCTLCMD; 3634 } 3635 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 3636 } 3637 #endif 3638 3639 const struct file_operations f2fs_file_operations = { 3640 .llseek = f2fs_llseek, 3641 .read_iter = f2fs_file_read_iter, 3642 .write_iter = f2fs_file_write_iter, 3643 .open = f2fs_file_open, 3644 .release = f2fs_release_file, 3645 .mmap = f2fs_file_mmap, 3646 .flush = f2fs_file_flush, 3647 .fsync = f2fs_sync_file, 3648 .fallocate = f2fs_fallocate, 3649 .unlocked_ioctl = f2fs_ioctl, 3650 #ifdef CONFIG_COMPAT 3651 .compat_ioctl = f2fs_compat_ioctl, 3652 #endif 3653 .splice_read = generic_file_splice_read, 3654 .splice_write = iter_file_splice_write, 3655 }; 3656