1 /* 2 * fs/f2fs/file.c 3 * 4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 5 * http://www.samsung.com/ 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 #include <linux/fs.h> 12 #include <linux/f2fs_fs.h> 13 #include <linux/stat.h> 14 #include <linux/buffer_head.h> 15 #include <linux/writeback.h> 16 #include <linux/blkdev.h> 17 #include <linux/falloc.h> 18 #include <linux/types.h> 19 #include <linux/compat.h> 20 #include <linux/uaccess.h> 21 #include <linux/mount.h> 22 #include <linux/pagevec.h> 23 #include <linux/random.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 int f2fs_vm_page_mkwrite(struct vm_area_struct *vma, 35 struct vm_fault *vmf) 36 { 37 struct page *page = vmf->page; 38 struct inode *inode = file_inode(vma->vm_file); 39 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 40 struct dnode_of_data dn; 41 int err; 42 43 sb_start_pagefault(inode->i_sb); 44 45 f2fs_bug_on(sbi, f2fs_has_inline_data(inode)); 46 47 /* block allocation */ 48 f2fs_lock_op(sbi); 49 set_new_dnode(&dn, inode, NULL, NULL, 0); 50 err = f2fs_reserve_block(&dn, page->index); 51 if (err) { 52 f2fs_unlock_op(sbi); 53 goto out; 54 } 55 f2fs_put_dnode(&dn); 56 f2fs_unlock_op(sbi); 57 58 f2fs_balance_fs(sbi, dn.node_changed); 59 60 file_update_time(vma->vm_file); 61 lock_page(page); 62 if (unlikely(page->mapping != inode->i_mapping || 63 page_offset(page) > i_size_read(inode) || 64 !PageUptodate(page))) { 65 unlock_page(page); 66 err = -EFAULT; 67 goto out; 68 } 69 70 /* 71 * check to see if the page is mapped already (no holes) 72 */ 73 if (PageMappedToDisk(page)) 74 goto mapped; 75 76 /* page is wholly or partially inside EOF */ 77 if (((loff_t)(page->index + 1) << PAGE_SHIFT) > 78 i_size_read(inode)) { 79 unsigned offset; 80 offset = i_size_read(inode) & ~PAGE_MASK; 81 zero_user_segment(page, offset, PAGE_SIZE); 82 } 83 set_page_dirty(page); 84 SetPageUptodate(page); 85 86 trace_f2fs_vm_page_mkwrite(page, DATA); 87 mapped: 88 /* fill the page */ 89 f2fs_wait_on_page_writeback(page, DATA, false); 90 91 /* wait for GCed encrypted page writeback */ 92 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) 93 f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr); 94 95 /* if gced page is attached, don't write to cold segment */ 96 clear_cold_data(page); 97 out: 98 sb_end_pagefault(inode->i_sb); 99 f2fs_update_time(sbi, REQ_TIME); 100 return block_page_mkwrite_return(err); 101 } 102 103 static const struct vm_operations_struct f2fs_file_vm_ops = { 104 .fault = filemap_fault, 105 .map_pages = filemap_map_pages, 106 .page_mkwrite = f2fs_vm_page_mkwrite, 107 }; 108 109 static int get_parent_ino(struct inode *inode, nid_t *pino) 110 { 111 struct dentry *dentry; 112 113 inode = igrab(inode); 114 dentry = d_find_any_alias(inode); 115 iput(inode); 116 if (!dentry) 117 return 0; 118 119 if (update_dent_inode(inode, inode, &dentry->d_name)) { 120 dput(dentry); 121 return 0; 122 } 123 124 *pino = parent_ino(dentry); 125 dput(dentry); 126 return 1; 127 } 128 129 static inline bool need_do_checkpoint(struct inode *inode) 130 { 131 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 132 bool need_cp = false; 133 134 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1) 135 need_cp = true; 136 else if (file_enc_name(inode) && need_dentry_mark(sbi, inode->i_ino)) 137 need_cp = true; 138 else if (file_wrong_pino(inode)) 139 need_cp = true; 140 else if (!space_for_roll_forward(sbi)) 141 need_cp = true; 142 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino)) 143 need_cp = true; 144 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi))) 145 need_cp = true; 146 else if (test_opt(sbi, FASTBOOT)) 147 need_cp = true; 148 else if (sbi->active_logs == 2) 149 need_cp = true; 150 151 return need_cp; 152 } 153 154 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino) 155 { 156 struct page *i = find_get_page(NODE_MAPPING(sbi), ino); 157 bool ret = false; 158 /* But we need to avoid that there are some inode updates */ 159 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino)) 160 ret = true; 161 f2fs_put_page(i, 0); 162 return ret; 163 } 164 165 static void try_to_fix_pino(struct inode *inode) 166 { 167 struct f2fs_inode_info *fi = F2FS_I(inode); 168 nid_t pino; 169 170 down_write(&fi->i_sem); 171 fi->xattr_ver = 0; 172 if (file_wrong_pino(inode) && inode->i_nlink == 1 && 173 get_parent_ino(inode, &pino)) { 174 fi->i_pino = pino; 175 file_got_pino(inode); 176 up_write(&fi->i_sem); 177 178 mark_inode_dirty_sync(inode); 179 f2fs_write_inode(inode, NULL); 180 } else { 181 up_write(&fi->i_sem); 182 } 183 } 184 185 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) 186 { 187 struct inode *inode = file->f_mapping->host; 188 struct f2fs_inode_info *fi = F2FS_I(inode); 189 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 190 nid_t ino = inode->i_ino; 191 int ret = 0; 192 bool need_cp = false; 193 struct writeback_control wbc = { 194 .sync_mode = WB_SYNC_ALL, 195 .nr_to_write = LONG_MAX, 196 .for_reclaim = 0, 197 }; 198 199 if (unlikely(f2fs_readonly(inode->i_sb))) 200 return 0; 201 202 trace_f2fs_sync_file_enter(inode); 203 204 /* if fdatasync is triggered, let's do in-place-update */ 205 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks) 206 set_inode_flag(fi, FI_NEED_IPU); 207 ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 208 clear_inode_flag(fi, FI_NEED_IPU); 209 210 if (ret) { 211 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); 212 return ret; 213 } 214 215 /* if the inode is dirty, let's recover all the time */ 216 if (!datasync) { 217 f2fs_write_inode(inode, NULL); 218 goto go_write; 219 } 220 221 /* 222 * if there is no written data, don't waste time to write recovery info. 223 */ 224 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) && 225 !exist_written_data(sbi, ino, APPEND_INO)) { 226 227 /* it may call write_inode just prior to fsync */ 228 if (need_inode_page_update(sbi, ino)) 229 goto go_write; 230 231 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) || 232 exist_written_data(sbi, ino, UPDATE_INO)) 233 goto flush_out; 234 goto out; 235 } 236 go_write: 237 /* 238 * Both of fdatasync() and fsync() are able to be recovered from 239 * sudden-power-off. 240 */ 241 down_read(&fi->i_sem); 242 need_cp = need_do_checkpoint(inode); 243 up_read(&fi->i_sem); 244 245 if (need_cp) { 246 /* all the dirty node pages should be flushed for POR */ 247 ret = f2fs_sync_fs(inode->i_sb, 1); 248 249 /* 250 * We've secured consistency through sync_fs. Following pino 251 * will be used only for fsynced inodes after checkpoint. 252 */ 253 try_to_fix_pino(inode); 254 clear_inode_flag(fi, FI_APPEND_WRITE); 255 clear_inode_flag(fi, FI_UPDATE_WRITE); 256 goto out; 257 } 258 sync_nodes: 259 sync_node_pages(sbi, ino, &wbc); 260 261 /* if cp_error was enabled, we should avoid infinite loop */ 262 if (unlikely(f2fs_cp_error(sbi))) { 263 ret = -EIO; 264 goto out; 265 } 266 267 if (need_inode_block_update(sbi, ino)) { 268 mark_inode_dirty_sync(inode); 269 f2fs_write_inode(inode, NULL); 270 goto sync_nodes; 271 } 272 273 ret = wait_on_node_pages_writeback(sbi, ino); 274 if (ret) 275 goto out; 276 277 /* once recovery info is written, don't need to tack this */ 278 remove_ino_entry(sbi, ino, APPEND_INO); 279 clear_inode_flag(fi, FI_APPEND_WRITE); 280 flush_out: 281 remove_ino_entry(sbi, ino, UPDATE_INO); 282 clear_inode_flag(fi, FI_UPDATE_WRITE); 283 ret = f2fs_issue_flush(sbi); 284 f2fs_update_time(sbi, REQ_TIME); 285 out: 286 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); 287 f2fs_trace_ios(NULL, 1); 288 return ret; 289 } 290 291 static pgoff_t __get_first_dirty_index(struct address_space *mapping, 292 pgoff_t pgofs, int whence) 293 { 294 struct pagevec pvec; 295 int nr_pages; 296 297 if (whence != SEEK_DATA) 298 return 0; 299 300 /* find first dirty page index */ 301 pagevec_init(&pvec, 0); 302 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs, 303 PAGECACHE_TAG_DIRTY, 1); 304 pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX; 305 pagevec_release(&pvec); 306 return pgofs; 307 } 308 309 static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs, 310 int whence) 311 { 312 switch (whence) { 313 case SEEK_DATA: 314 if ((blkaddr == NEW_ADDR && dirty == pgofs) || 315 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR)) 316 return true; 317 break; 318 case SEEK_HOLE: 319 if (blkaddr == NULL_ADDR) 320 return true; 321 break; 322 } 323 return false; 324 } 325 326 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) 327 { 328 struct inode *inode = file->f_mapping->host; 329 loff_t maxbytes = inode->i_sb->s_maxbytes; 330 struct dnode_of_data dn; 331 pgoff_t pgofs, end_offset, dirty; 332 loff_t data_ofs = offset; 333 loff_t isize; 334 int err = 0; 335 336 inode_lock(inode); 337 338 isize = i_size_read(inode); 339 if (offset >= isize) 340 goto fail; 341 342 /* handle inline data case */ 343 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) { 344 if (whence == SEEK_HOLE) 345 data_ofs = isize; 346 goto found; 347 } 348 349 pgofs = (pgoff_t)(offset >> PAGE_SHIFT); 350 351 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence); 352 353 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) { 354 set_new_dnode(&dn, inode, NULL, NULL, 0); 355 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA); 356 if (err && err != -ENOENT) { 357 goto fail; 358 } else if (err == -ENOENT) { 359 /* direct node does not exists */ 360 if (whence == SEEK_DATA) { 361 pgofs = get_next_page_offset(&dn, pgofs); 362 continue; 363 } else { 364 goto found; 365 } 366 } 367 368 end_offset = ADDRS_PER_PAGE(dn.node_page, inode); 369 370 /* find data/hole in dnode block */ 371 for (; dn.ofs_in_node < end_offset; 372 dn.ofs_in_node++, pgofs++, 373 data_ofs = (loff_t)pgofs << PAGE_SHIFT) { 374 block_t blkaddr; 375 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); 376 377 if (__found_offset(blkaddr, dirty, pgofs, whence)) { 378 f2fs_put_dnode(&dn); 379 goto found; 380 } 381 } 382 f2fs_put_dnode(&dn); 383 } 384 385 if (whence == SEEK_DATA) 386 goto fail; 387 found: 388 if (whence == SEEK_HOLE && data_ofs > isize) 389 data_ofs = isize; 390 inode_unlock(inode); 391 return vfs_setpos(file, data_ofs, maxbytes); 392 fail: 393 inode_unlock(inode); 394 return -ENXIO; 395 } 396 397 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence) 398 { 399 struct inode *inode = file->f_mapping->host; 400 loff_t maxbytes = inode->i_sb->s_maxbytes; 401 402 switch (whence) { 403 case SEEK_SET: 404 case SEEK_CUR: 405 case SEEK_END: 406 return generic_file_llseek_size(file, offset, whence, 407 maxbytes, i_size_read(inode)); 408 case SEEK_DATA: 409 case SEEK_HOLE: 410 if (offset < 0) 411 return -ENXIO; 412 return f2fs_seek_block(file, offset, whence); 413 } 414 415 return -EINVAL; 416 } 417 418 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) 419 { 420 struct inode *inode = file_inode(file); 421 int err; 422 423 if (f2fs_encrypted_inode(inode)) { 424 err = fscrypt_get_encryption_info(inode); 425 if (err) 426 return 0; 427 if (!f2fs_encrypted_inode(inode)) 428 return -ENOKEY; 429 } 430 431 /* we don't need to use inline_data strictly */ 432 err = f2fs_convert_inline_inode(inode); 433 if (err) 434 return err; 435 436 file_accessed(file); 437 vma->vm_ops = &f2fs_file_vm_ops; 438 return 0; 439 } 440 441 static int f2fs_file_open(struct inode *inode, struct file *filp) 442 { 443 int ret = generic_file_open(inode, filp); 444 struct inode *dir = filp->f_path.dentry->d_parent->d_inode; 445 446 if (!ret && f2fs_encrypted_inode(inode)) { 447 ret = fscrypt_get_encryption_info(inode); 448 if (ret) 449 return -EACCES; 450 if (!fscrypt_has_encryption_key(inode)) 451 return -ENOKEY; 452 } 453 if (f2fs_encrypted_inode(dir) && 454 !fscrypt_has_permitted_context(dir, inode)) 455 return -EPERM; 456 return ret; 457 } 458 459 int truncate_data_blocks_range(struct dnode_of_data *dn, int count) 460 { 461 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 462 struct f2fs_node *raw_node; 463 int nr_free = 0, ofs = dn->ofs_in_node, len = count; 464 __le32 *addr; 465 466 raw_node = F2FS_NODE(dn->node_page); 467 addr = blkaddr_in_node(raw_node) + ofs; 468 469 for (; count > 0; count--, addr++, dn->ofs_in_node++) { 470 block_t blkaddr = le32_to_cpu(*addr); 471 if (blkaddr == NULL_ADDR) 472 continue; 473 474 dn->data_blkaddr = NULL_ADDR; 475 set_data_blkaddr(dn); 476 invalidate_blocks(sbi, blkaddr); 477 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page)) 478 clear_inode_flag(F2FS_I(dn->inode), 479 FI_FIRST_BLOCK_WRITTEN); 480 nr_free++; 481 } 482 483 if (nr_free) { 484 pgoff_t fofs; 485 /* 486 * once we invalidate valid blkaddr in range [ofs, ofs + count], 487 * we will invalidate all blkaddr in the whole range. 488 */ 489 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), 490 dn->inode) + ofs; 491 f2fs_update_extent_cache_range(dn, fofs, 0, len); 492 dec_valid_block_count(sbi, dn->inode, nr_free); 493 sync_inode_page(dn); 494 } 495 dn->ofs_in_node = ofs; 496 497 f2fs_update_time(sbi, REQ_TIME); 498 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid, 499 dn->ofs_in_node, nr_free); 500 return nr_free; 501 } 502 503 void truncate_data_blocks(struct dnode_of_data *dn) 504 { 505 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK); 506 } 507 508 static int truncate_partial_data_page(struct inode *inode, u64 from, 509 bool cache_only) 510 { 511 unsigned offset = from & (PAGE_SIZE - 1); 512 pgoff_t index = from >> PAGE_SHIFT; 513 struct address_space *mapping = inode->i_mapping; 514 struct page *page; 515 516 if (!offset && !cache_only) 517 return 0; 518 519 if (cache_only) { 520 page = f2fs_grab_cache_page(mapping, index, false); 521 if (page && PageUptodate(page)) 522 goto truncate_out; 523 f2fs_put_page(page, 1); 524 return 0; 525 } 526 527 page = get_lock_data_page(inode, index, true); 528 if (IS_ERR(page)) 529 return 0; 530 truncate_out: 531 f2fs_wait_on_page_writeback(page, DATA, true); 532 zero_user(page, offset, PAGE_SIZE - offset); 533 if (!cache_only || !f2fs_encrypted_inode(inode) || 534 !S_ISREG(inode->i_mode)) 535 set_page_dirty(page); 536 f2fs_put_page(page, 1); 537 return 0; 538 } 539 540 int truncate_blocks(struct inode *inode, u64 from, bool lock) 541 { 542 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 543 unsigned int blocksize = inode->i_sb->s_blocksize; 544 struct dnode_of_data dn; 545 pgoff_t free_from; 546 int count = 0, err = 0; 547 struct page *ipage; 548 bool truncate_page = false; 549 550 trace_f2fs_truncate_blocks_enter(inode, from); 551 552 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1); 553 554 if (lock) 555 f2fs_lock_op(sbi); 556 557 ipage = get_node_page(sbi, inode->i_ino); 558 if (IS_ERR(ipage)) { 559 err = PTR_ERR(ipage); 560 goto out; 561 } 562 563 if (f2fs_has_inline_data(inode)) { 564 if (truncate_inline_inode(ipage, from)) 565 set_page_dirty(ipage); 566 f2fs_put_page(ipage, 1); 567 truncate_page = true; 568 goto out; 569 } 570 571 set_new_dnode(&dn, inode, ipage, NULL, 0); 572 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE); 573 if (err) { 574 if (err == -ENOENT) 575 goto free_next; 576 goto out; 577 } 578 579 count = ADDRS_PER_PAGE(dn.node_page, inode); 580 581 count -= dn.ofs_in_node; 582 f2fs_bug_on(sbi, count < 0); 583 584 if (dn.ofs_in_node || IS_INODE(dn.node_page)) { 585 truncate_data_blocks_range(&dn, count); 586 free_from += count; 587 } 588 589 f2fs_put_dnode(&dn); 590 free_next: 591 err = truncate_inode_blocks(inode, free_from); 592 out: 593 if (lock) 594 f2fs_unlock_op(sbi); 595 596 /* lastly zero out the first data page */ 597 if (!err) 598 err = truncate_partial_data_page(inode, from, truncate_page); 599 600 trace_f2fs_truncate_blocks_exit(inode, err); 601 return err; 602 } 603 604 int f2fs_truncate(struct inode *inode, bool lock) 605 { 606 int err; 607 608 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 609 S_ISLNK(inode->i_mode))) 610 return 0; 611 612 trace_f2fs_truncate(inode); 613 614 /* we should check inline_data size */ 615 if (!f2fs_may_inline_data(inode)) { 616 err = f2fs_convert_inline_inode(inode); 617 if (err) 618 return err; 619 } 620 621 err = truncate_blocks(inode, i_size_read(inode), lock); 622 if (err) 623 return err; 624 625 inode->i_mtime = inode->i_ctime = CURRENT_TIME; 626 mark_inode_dirty(inode); 627 return 0; 628 } 629 630 int f2fs_getattr(struct vfsmount *mnt, 631 struct dentry *dentry, struct kstat *stat) 632 { 633 struct inode *inode = d_inode(dentry); 634 generic_fillattr(inode, stat); 635 stat->blocks <<= 3; 636 return 0; 637 } 638 639 #ifdef CONFIG_F2FS_FS_POSIX_ACL 640 static void __setattr_copy(struct inode *inode, const struct iattr *attr) 641 { 642 struct f2fs_inode_info *fi = F2FS_I(inode); 643 unsigned int ia_valid = attr->ia_valid; 644 645 if (ia_valid & ATTR_UID) 646 inode->i_uid = attr->ia_uid; 647 if (ia_valid & ATTR_GID) 648 inode->i_gid = attr->ia_gid; 649 if (ia_valid & ATTR_ATIME) 650 inode->i_atime = timespec_trunc(attr->ia_atime, 651 inode->i_sb->s_time_gran); 652 if (ia_valid & ATTR_MTIME) 653 inode->i_mtime = timespec_trunc(attr->ia_mtime, 654 inode->i_sb->s_time_gran); 655 if (ia_valid & ATTR_CTIME) 656 inode->i_ctime = timespec_trunc(attr->ia_ctime, 657 inode->i_sb->s_time_gran); 658 if (ia_valid & ATTR_MODE) { 659 umode_t mode = attr->ia_mode; 660 661 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) 662 mode &= ~S_ISGID; 663 set_acl_inode(fi, mode); 664 } 665 } 666 #else 667 #define __setattr_copy setattr_copy 668 #endif 669 670 int f2fs_setattr(struct dentry *dentry, struct iattr *attr) 671 { 672 struct inode *inode = d_inode(dentry); 673 struct f2fs_inode_info *fi = F2FS_I(inode); 674 int err; 675 676 err = inode_change_ok(inode, attr); 677 if (err) 678 return err; 679 680 if (attr->ia_valid & ATTR_SIZE) { 681 if (f2fs_encrypted_inode(inode) && 682 fscrypt_get_encryption_info(inode)) 683 return -EACCES; 684 685 if (attr->ia_size <= i_size_read(inode)) { 686 truncate_setsize(inode, attr->ia_size); 687 err = f2fs_truncate(inode, true); 688 if (err) 689 return err; 690 f2fs_balance_fs(F2FS_I_SB(inode), true); 691 } else { 692 /* 693 * do not trim all blocks after i_size if target size is 694 * larger than i_size. 695 */ 696 truncate_setsize(inode, attr->ia_size); 697 698 /* should convert inline inode here */ 699 if (!f2fs_may_inline_data(inode)) { 700 err = f2fs_convert_inline_inode(inode); 701 if (err) 702 return err; 703 } 704 inode->i_mtime = inode->i_ctime = CURRENT_TIME; 705 } 706 } 707 708 __setattr_copy(inode, attr); 709 710 if (attr->ia_valid & ATTR_MODE) { 711 err = posix_acl_chmod(inode, get_inode_mode(inode)); 712 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) { 713 inode->i_mode = fi->i_acl_mode; 714 clear_inode_flag(fi, FI_ACL_MODE); 715 } 716 } 717 718 mark_inode_dirty(inode); 719 return err; 720 } 721 722 const struct inode_operations f2fs_file_inode_operations = { 723 .getattr = f2fs_getattr, 724 .setattr = f2fs_setattr, 725 .get_acl = f2fs_get_acl, 726 .set_acl = f2fs_set_acl, 727 #ifdef CONFIG_F2FS_FS_XATTR 728 .setxattr = generic_setxattr, 729 .getxattr = generic_getxattr, 730 .listxattr = f2fs_listxattr, 731 .removexattr = generic_removexattr, 732 #endif 733 .fiemap = f2fs_fiemap, 734 }; 735 736 static int fill_zero(struct inode *inode, pgoff_t index, 737 loff_t start, loff_t len) 738 { 739 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 740 struct page *page; 741 742 if (!len) 743 return 0; 744 745 f2fs_balance_fs(sbi, true); 746 747 f2fs_lock_op(sbi); 748 page = get_new_data_page(inode, NULL, index, false); 749 f2fs_unlock_op(sbi); 750 751 if (IS_ERR(page)) 752 return PTR_ERR(page); 753 754 f2fs_wait_on_page_writeback(page, DATA, true); 755 zero_user(page, start, len); 756 set_page_dirty(page); 757 f2fs_put_page(page, 1); 758 return 0; 759 } 760 761 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) 762 { 763 int err; 764 765 while (pg_start < pg_end) { 766 struct dnode_of_data dn; 767 pgoff_t end_offset, count; 768 769 set_new_dnode(&dn, inode, NULL, NULL, 0); 770 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE); 771 if (err) { 772 if (err == -ENOENT) { 773 pg_start++; 774 continue; 775 } 776 return err; 777 } 778 779 end_offset = ADDRS_PER_PAGE(dn.node_page, inode); 780 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start); 781 782 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset); 783 784 truncate_data_blocks_range(&dn, count); 785 f2fs_put_dnode(&dn); 786 787 pg_start += count; 788 } 789 return 0; 790 } 791 792 static int punch_hole(struct inode *inode, loff_t offset, loff_t len) 793 { 794 pgoff_t pg_start, pg_end; 795 loff_t off_start, off_end; 796 int ret; 797 798 ret = f2fs_convert_inline_inode(inode); 799 if (ret) 800 return ret; 801 802 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT; 803 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT; 804 805 off_start = offset & (PAGE_SIZE - 1); 806 off_end = (offset + len) & (PAGE_SIZE - 1); 807 808 if (pg_start == pg_end) { 809 ret = fill_zero(inode, pg_start, off_start, 810 off_end - off_start); 811 if (ret) 812 return ret; 813 } else { 814 if (off_start) { 815 ret = fill_zero(inode, pg_start++, off_start, 816 PAGE_SIZE - off_start); 817 if (ret) 818 return ret; 819 } 820 if (off_end) { 821 ret = fill_zero(inode, pg_end, 0, off_end); 822 if (ret) 823 return ret; 824 } 825 826 if (pg_start < pg_end) { 827 struct address_space *mapping = inode->i_mapping; 828 loff_t blk_start, blk_end; 829 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 830 831 f2fs_balance_fs(sbi, true); 832 833 blk_start = (loff_t)pg_start << PAGE_SHIFT; 834 blk_end = (loff_t)pg_end << PAGE_SHIFT; 835 truncate_inode_pages_range(mapping, blk_start, 836 blk_end - 1); 837 838 f2fs_lock_op(sbi); 839 ret = truncate_hole(inode, pg_start, pg_end); 840 f2fs_unlock_op(sbi); 841 } 842 } 843 844 return ret; 845 } 846 847 static int __exchange_data_block(struct inode *inode, pgoff_t src, 848 pgoff_t dst, bool full) 849 { 850 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 851 struct dnode_of_data dn; 852 block_t new_addr; 853 bool do_replace = false; 854 int ret; 855 856 set_new_dnode(&dn, inode, NULL, NULL, 0); 857 ret = get_dnode_of_data(&dn, src, LOOKUP_NODE_RA); 858 if (ret && ret != -ENOENT) { 859 return ret; 860 } else if (ret == -ENOENT) { 861 new_addr = NULL_ADDR; 862 } else { 863 new_addr = dn.data_blkaddr; 864 if (!is_checkpointed_data(sbi, new_addr)) { 865 /* do not invalidate this block address */ 866 f2fs_update_data_blkaddr(&dn, NULL_ADDR); 867 do_replace = true; 868 } 869 f2fs_put_dnode(&dn); 870 } 871 872 if (new_addr == NULL_ADDR) 873 return full ? truncate_hole(inode, dst, dst + 1) : 0; 874 875 if (do_replace) { 876 struct page *ipage = get_node_page(sbi, inode->i_ino); 877 struct node_info ni; 878 879 if (IS_ERR(ipage)) { 880 ret = PTR_ERR(ipage); 881 goto err_out; 882 } 883 884 set_new_dnode(&dn, inode, ipage, NULL, 0); 885 ret = f2fs_reserve_block(&dn, dst); 886 if (ret) 887 goto err_out; 888 889 truncate_data_blocks_range(&dn, 1); 890 891 get_node_info(sbi, dn.nid, &ni); 892 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, new_addr, 893 ni.version, true, false); 894 f2fs_put_dnode(&dn); 895 } else { 896 struct page *psrc, *pdst; 897 898 psrc = get_lock_data_page(inode, src, true); 899 if (IS_ERR(psrc)) 900 return PTR_ERR(psrc); 901 pdst = get_new_data_page(inode, NULL, dst, true); 902 if (IS_ERR(pdst)) { 903 f2fs_put_page(psrc, 1); 904 return PTR_ERR(pdst); 905 } 906 f2fs_copy_page(psrc, pdst); 907 set_page_dirty(pdst); 908 f2fs_put_page(pdst, 1); 909 f2fs_put_page(psrc, 1); 910 911 return truncate_hole(inode, src, src + 1); 912 } 913 return 0; 914 915 err_out: 916 if (!get_dnode_of_data(&dn, src, LOOKUP_NODE)) { 917 f2fs_update_data_blkaddr(&dn, new_addr); 918 f2fs_put_dnode(&dn); 919 } 920 return ret; 921 } 922 923 static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end) 924 { 925 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 926 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE; 927 int ret = 0; 928 929 for (; end < nrpages; start++, end++) { 930 f2fs_balance_fs(sbi, true); 931 f2fs_lock_op(sbi); 932 ret = __exchange_data_block(inode, end, start, true); 933 f2fs_unlock_op(sbi); 934 if (ret) 935 break; 936 } 937 return ret; 938 } 939 940 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len) 941 { 942 pgoff_t pg_start, pg_end; 943 loff_t new_size; 944 int ret; 945 946 if (offset + len >= i_size_read(inode)) 947 return -EINVAL; 948 949 /* collapse range should be aligned to block size of f2fs. */ 950 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) 951 return -EINVAL; 952 953 ret = f2fs_convert_inline_inode(inode); 954 if (ret) 955 return ret; 956 957 pg_start = offset >> PAGE_SHIFT; 958 pg_end = (offset + len) >> PAGE_SHIFT; 959 960 /* write out all dirty pages from offset */ 961 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 962 if (ret) 963 return ret; 964 965 truncate_pagecache(inode, offset); 966 967 ret = f2fs_do_collapse(inode, pg_start, pg_end); 968 if (ret) 969 return ret; 970 971 /* write out all moved pages, if possible */ 972 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 973 truncate_pagecache(inode, offset); 974 975 new_size = i_size_read(inode) - len; 976 truncate_pagecache(inode, new_size); 977 978 ret = truncate_blocks(inode, new_size, true); 979 if (!ret) 980 i_size_write(inode, new_size); 981 982 return ret; 983 } 984 985 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len, 986 int mode) 987 { 988 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 989 struct address_space *mapping = inode->i_mapping; 990 pgoff_t index, pg_start, pg_end; 991 loff_t new_size = i_size_read(inode); 992 loff_t off_start, off_end; 993 int ret = 0; 994 995 ret = inode_newsize_ok(inode, (len + offset)); 996 if (ret) 997 return ret; 998 999 ret = f2fs_convert_inline_inode(inode); 1000 if (ret) 1001 return ret; 1002 1003 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1); 1004 if (ret) 1005 return ret; 1006 1007 truncate_pagecache_range(inode, offset, offset + len - 1); 1008 1009 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT; 1010 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT; 1011 1012 off_start = offset & (PAGE_SIZE - 1); 1013 off_end = (offset + len) & (PAGE_SIZE - 1); 1014 1015 if (pg_start == pg_end) { 1016 ret = fill_zero(inode, pg_start, off_start, 1017 off_end - off_start); 1018 if (ret) 1019 return ret; 1020 1021 if (offset + len > new_size) 1022 new_size = offset + len; 1023 new_size = max_t(loff_t, new_size, offset + len); 1024 } else { 1025 if (off_start) { 1026 ret = fill_zero(inode, pg_start++, off_start, 1027 PAGE_SIZE - off_start); 1028 if (ret) 1029 return ret; 1030 1031 new_size = max_t(loff_t, new_size, 1032 (loff_t)pg_start << PAGE_SHIFT); 1033 } 1034 1035 for (index = pg_start; index < pg_end; index++) { 1036 struct dnode_of_data dn; 1037 struct page *ipage; 1038 1039 f2fs_lock_op(sbi); 1040 1041 ipage = get_node_page(sbi, inode->i_ino); 1042 if (IS_ERR(ipage)) { 1043 ret = PTR_ERR(ipage); 1044 f2fs_unlock_op(sbi); 1045 goto out; 1046 } 1047 1048 set_new_dnode(&dn, inode, ipage, NULL, 0); 1049 ret = f2fs_reserve_block(&dn, index); 1050 if (ret) { 1051 f2fs_unlock_op(sbi); 1052 goto out; 1053 } 1054 1055 if (dn.data_blkaddr != NEW_ADDR) { 1056 invalidate_blocks(sbi, dn.data_blkaddr); 1057 f2fs_update_data_blkaddr(&dn, NEW_ADDR); 1058 } 1059 f2fs_put_dnode(&dn); 1060 f2fs_unlock_op(sbi); 1061 1062 new_size = max_t(loff_t, new_size, 1063 (loff_t)(index + 1) << PAGE_SHIFT); 1064 } 1065 1066 if (off_end) { 1067 ret = fill_zero(inode, pg_end, 0, off_end); 1068 if (ret) 1069 goto out; 1070 1071 new_size = max_t(loff_t, new_size, offset + len); 1072 } 1073 } 1074 1075 out: 1076 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size) { 1077 i_size_write(inode, new_size); 1078 mark_inode_dirty(inode); 1079 update_inode_page(inode); 1080 } 1081 1082 return ret; 1083 } 1084 1085 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len) 1086 { 1087 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1088 pgoff_t pg_start, pg_end, delta, nrpages, idx; 1089 loff_t new_size; 1090 int ret = 0; 1091 1092 new_size = i_size_read(inode) + len; 1093 if (new_size > inode->i_sb->s_maxbytes) 1094 return -EFBIG; 1095 1096 if (offset >= i_size_read(inode)) 1097 return -EINVAL; 1098 1099 /* insert range should be aligned to block size of f2fs. */ 1100 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) 1101 return -EINVAL; 1102 1103 ret = f2fs_convert_inline_inode(inode); 1104 if (ret) 1105 return ret; 1106 1107 f2fs_balance_fs(sbi, true); 1108 1109 ret = truncate_blocks(inode, i_size_read(inode), true); 1110 if (ret) 1111 return ret; 1112 1113 /* write out all dirty pages from offset */ 1114 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 1115 if (ret) 1116 return ret; 1117 1118 truncate_pagecache(inode, offset); 1119 1120 pg_start = offset >> PAGE_SHIFT; 1121 pg_end = (offset + len) >> PAGE_SHIFT; 1122 delta = pg_end - pg_start; 1123 nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE; 1124 1125 for (idx = nrpages - 1; idx >= pg_start && idx != -1; idx--) { 1126 f2fs_lock_op(sbi); 1127 ret = __exchange_data_block(inode, idx, idx + delta, false); 1128 f2fs_unlock_op(sbi); 1129 if (ret) 1130 break; 1131 } 1132 1133 /* write out all moved pages, if possible */ 1134 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 1135 truncate_pagecache(inode, offset); 1136 1137 if (!ret) 1138 i_size_write(inode, new_size); 1139 return ret; 1140 } 1141 1142 static int expand_inode_data(struct inode *inode, loff_t offset, 1143 loff_t len, int mode) 1144 { 1145 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1146 pgoff_t index, pg_start, pg_end; 1147 loff_t new_size = i_size_read(inode); 1148 loff_t off_start, off_end; 1149 int ret = 0; 1150 1151 ret = inode_newsize_ok(inode, (len + offset)); 1152 if (ret) 1153 return ret; 1154 1155 ret = f2fs_convert_inline_inode(inode); 1156 if (ret) 1157 return ret; 1158 1159 f2fs_balance_fs(sbi, true); 1160 1161 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT; 1162 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT; 1163 1164 off_start = offset & (PAGE_SIZE - 1); 1165 off_end = (offset + len) & (PAGE_SIZE - 1); 1166 1167 f2fs_lock_op(sbi); 1168 1169 for (index = pg_start; index <= pg_end; index++) { 1170 struct dnode_of_data dn; 1171 1172 if (index == pg_end && !off_end) 1173 goto noalloc; 1174 1175 set_new_dnode(&dn, inode, NULL, NULL, 0); 1176 ret = f2fs_reserve_block(&dn, index); 1177 if (ret) 1178 break; 1179 noalloc: 1180 if (pg_start == pg_end) 1181 new_size = offset + len; 1182 else if (index == pg_start && off_start) 1183 new_size = (loff_t)(index + 1) << PAGE_SHIFT; 1184 else if (index == pg_end) 1185 new_size = ((loff_t)index << PAGE_SHIFT) + 1186 off_end; 1187 else 1188 new_size += PAGE_SIZE; 1189 } 1190 1191 if (!(mode & FALLOC_FL_KEEP_SIZE) && 1192 i_size_read(inode) < new_size) { 1193 i_size_write(inode, new_size); 1194 mark_inode_dirty(inode); 1195 update_inode_page(inode); 1196 } 1197 f2fs_unlock_op(sbi); 1198 1199 return ret; 1200 } 1201 1202 static long f2fs_fallocate(struct file *file, int mode, 1203 loff_t offset, loff_t len) 1204 { 1205 struct inode *inode = file_inode(file); 1206 long ret = 0; 1207 1208 /* f2fs only support ->fallocate for regular file */ 1209 if (!S_ISREG(inode->i_mode)) 1210 return -EINVAL; 1211 1212 if (f2fs_encrypted_inode(inode) && 1213 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) 1214 return -EOPNOTSUPP; 1215 1216 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | 1217 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | 1218 FALLOC_FL_INSERT_RANGE)) 1219 return -EOPNOTSUPP; 1220 1221 inode_lock(inode); 1222 1223 if (mode & FALLOC_FL_PUNCH_HOLE) { 1224 if (offset >= inode->i_size) 1225 goto out; 1226 1227 ret = punch_hole(inode, offset, len); 1228 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { 1229 ret = f2fs_collapse_range(inode, offset, len); 1230 } else if (mode & FALLOC_FL_ZERO_RANGE) { 1231 ret = f2fs_zero_range(inode, offset, len, mode); 1232 } else if (mode & FALLOC_FL_INSERT_RANGE) { 1233 ret = f2fs_insert_range(inode, offset, len); 1234 } else { 1235 ret = expand_inode_data(inode, offset, len, mode); 1236 } 1237 1238 if (!ret) { 1239 inode->i_mtime = inode->i_ctime = CURRENT_TIME; 1240 mark_inode_dirty(inode); 1241 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1242 } 1243 1244 out: 1245 inode_unlock(inode); 1246 1247 trace_f2fs_fallocate(inode, mode, offset, len, ret); 1248 return ret; 1249 } 1250 1251 static int f2fs_release_file(struct inode *inode, struct file *filp) 1252 { 1253 /* some remained atomic pages should discarded */ 1254 if (f2fs_is_atomic_file(inode)) 1255 drop_inmem_pages(inode); 1256 if (f2fs_is_volatile_file(inode)) { 1257 set_inode_flag(F2FS_I(inode), FI_DROP_CACHE); 1258 filemap_fdatawrite(inode->i_mapping); 1259 clear_inode_flag(F2FS_I(inode), FI_DROP_CACHE); 1260 } 1261 return 0; 1262 } 1263 1264 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL)) 1265 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL) 1266 1267 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags) 1268 { 1269 if (S_ISDIR(mode)) 1270 return flags; 1271 else if (S_ISREG(mode)) 1272 return flags & F2FS_REG_FLMASK; 1273 else 1274 return flags & F2FS_OTHER_FLMASK; 1275 } 1276 1277 static int f2fs_ioc_getflags(struct file *filp, unsigned long arg) 1278 { 1279 struct inode *inode = file_inode(filp); 1280 struct f2fs_inode_info *fi = F2FS_I(inode); 1281 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE; 1282 return put_user(flags, (int __user *)arg); 1283 } 1284 1285 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg) 1286 { 1287 struct inode *inode = file_inode(filp); 1288 struct f2fs_inode_info *fi = F2FS_I(inode); 1289 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE; 1290 unsigned int oldflags; 1291 int ret; 1292 1293 ret = mnt_want_write_file(filp); 1294 if (ret) 1295 return ret; 1296 1297 if (!inode_owner_or_capable(inode)) { 1298 ret = -EACCES; 1299 goto out; 1300 } 1301 1302 if (get_user(flags, (int __user *)arg)) { 1303 ret = -EFAULT; 1304 goto out; 1305 } 1306 1307 flags = f2fs_mask_flags(inode->i_mode, flags); 1308 1309 inode_lock(inode); 1310 1311 oldflags = fi->i_flags; 1312 1313 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 1314 if (!capable(CAP_LINUX_IMMUTABLE)) { 1315 inode_unlock(inode); 1316 ret = -EPERM; 1317 goto out; 1318 } 1319 } 1320 1321 flags = flags & FS_FL_USER_MODIFIABLE; 1322 flags |= oldflags & ~FS_FL_USER_MODIFIABLE; 1323 fi->i_flags = flags; 1324 inode_unlock(inode); 1325 1326 f2fs_set_inode_flags(inode); 1327 inode->i_ctime = CURRENT_TIME; 1328 mark_inode_dirty(inode); 1329 out: 1330 mnt_drop_write_file(filp); 1331 return ret; 1332 } 1333 1334 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg) 1335 { 1336 struct inode *inode = file_inode(filp); 1337 1338 return put_user(inode->i_generation, (int __user *)arg); 1339 } 1340 1341 static int f2fs_ioc_start_atomic_write(struct file *filp) 1342 { 1343 struct inode *inode = file_inode(filp); 1344 int ret; 1345 1346 if (!inode_owner_or_capable(inode)) 1347 return -EACCES; 1348 1349 if (f2fs_is_atomic_file(inode)) 1350 return 0; 1351 1352 ret = f2fs_convert_inline_inode(inode); 1353 if (ret) 1354 return ret; 1355 1356 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); 1357 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1358 1359 return 0; 1360 } 1361 1362 static int f2fs_ioc_commit_atomic_write(struct file *filp) 1363 { 1364 struct inode *inode = file_inode(filp); 1365 int ret; 1366 1367 if (!inode_owner_or_capable(inode)) 1368 return -EACCES; 1369 1370 if (f2fs_is_volatile_file(inode)) 1371 return 0; 1372 1373 ret = mnt_want_write_file(filp); 1374 if (ret) 1375 return ret; 1376 1377 if (f2fs_is_atomic_file(inode)) { 1378 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); 1379 ret = commit_inmem_pages(inode); 1380 if (ret) { 1381 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); 1382 goto err_out; 1383 } 1384 } 1385 1386 ret = f2fs_sync_file(filp, 0, LLONG_MAX, 0); 1387 err_out: 1388 mnt_drop_write_file(filp); 1389 return ret; 1390 } 1391 1392 static int f2fs_ioc_start_volatile_write(struct file *filp) 1393 { 1394 struct inode *inode = file_inode(filp); 1395 int ret; 1396 1397 if (!inode_owner_or_capable(inode)) 1398 return -EACCES; 1399 1400 if (f2fs_is_volatile_file(inode)) 1401 return 0; 1402 1403 ret = f2fs_convert_inline_inode(inode); 1404 if (ret) 1405 return ret; 1406 1407 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE); 1408 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1409 return 0; 1410 } 1411 1412 static int f2fs_ioc_release_volatile_write(struct file *filp) 1413 { 1414 struct inode *inode = file_inode(filp); 1415 1416 if (!inode_owner_or_capable(inode)) 1417 return -EACCES; 1418 1419 if (!f2fs_is_volatile_file(inode)) 1420 return 0; 1421 1422 if (!f2fs_is_first_block_written(inode)) 1423 return truncate_partial_data_page(inode, 0, true); 1424 1425 return punch_hole(inode, 0, F2FS_BLKSIZE); 1426 } 1427 1428 static int f2fs_ioc_abort_volatile_write(struct file *filp) 1429 { 1430 struct inode *inode = file_inode(filp); 1431 int ret; 1432 1433 if (!inode_owner_or_capable(inode)) 1434 return -EACCES; 1435 1436 ret = mnt_want_write_file(filp); 1437 if (ret) 1438 return ret; 1439 1440 if (f2fs_is_atomic_file(inode)) { 1441 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); 1442 drop_inmem_pages(inode); 1443 } 1444 if (f2fs_is_volatile_file(inode)) { 1445 clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE); 1446 ret = f2fs_sync_file(filp, 0, LLONG_MAX, 0); 1447 } 1448 1449 mnt_drop_write_file(filp); 1450 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1451 return ret; 1452 } 1453 1454 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg) 1455 { 1456 struct inode *inode = file_inode(filp); 1457 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1458 struct super_block *sb = sbi->sb; 1459 __u32 in; 1460 1461 if (!capable(CAP_SYS_ADMIN)) 1462 return -EPERM; 1463 1464 if (get_user(in, (__u32 __user *)arg)) 1465 return -EFAULT; 1466 1467 switch (in) { 1468 case F2FS_GOING_DOWN_FULLSYNC: 1469 sb = freeze_bdev(sb->s_bdev); 1470 if (sb && !IS_ERR(sb)) { 1471 f2fs_stop_checkpoint(sbi); 1472 thaw_bdev(sb->s_bdev, sb); 1473 } 1474 break; 1475 case F2FS_GOING_DOWN_METASYNC: 1476 /* do checkpoint only */ 1477 f2fs_sync_fs(sb, 1); 1478 f2fs_stop_checkpoint(sbi); 1479 break; 1480 case F2FS_GOING_DOWN_NOSYNC: 1481 f2fs_stop_checkpoint(sbi); 1482 break; 1483 case F2FS_GOING_DOWN_METAFLUSH: 1484 sync_meta_pages(sbi, META, LONG_MAX); 1485 f2fs_stop_checkpoint(sbi); 1486 break; 1487 default: 1488 return -EINVAL; 1489 } 1490 f2fs_update_time(sbi, REQ_TIME); 1491 return 0; 1492 } 1493 1494 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg) 1495 { 1496 struct inode *inode = file_inode(filp); 1497 struct super_block *sb = inode->i_sb; 1498 struct request_queue *q = bdev_get_queue(sb->s_bdev); 1499 struct fstrim_range range; 1500 int ret; 1501 1502 if (!capable(CAP_SYS_ADMIN)) 1503 return -EPERM; 1504 1505 if (!blk_queue_discard(q)) 1506 return -EOPNOTSUPP; 1507 1508 if (copy_from_user(&range, (struct fstrim_range __user *)arg, 1509 sizeof(range))) 1510 return -EFAULT; 1511 1512 range.minlen = max((unsigned int)range.minlen, 1513 q->limits.discard_granularity); 1514 ret = f2fs_trim_fs(F2FS_SB(sb), &range); 1515 if (ret < 0) 1516 return ret; 1517 1518 if (copy_to_user((struct fstrim_range __user *)arg, &range, 1519 sizeof(range))) 1520 return -EFAULT; 1521 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1522 return 0; 1523 } 1524 1525 static bool uuid_is_nonzero(__u8 u[16]) 1526 { 1527 int i; 1528 1529 for (i = 0; i < 16; i++) 1530 if (u[i]) 1531 return true; 1532 return false; 1533 } 1534 1535 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg) 1536 { 1537 struct fscrypt_policy policy; 1538 struct inode *inode = file_inode(filp); 1539 1540 if (copy_from_user(&policy, (struct fscrypt_policy __user *)arg, 1541 sizeof(policy))) 1542 return -EFAULT; 1543 1544 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1545 return fscrypt_process_policy(inode, &policy); 1546 } 1547 1548 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg) 1549 { 1550 struct fscrypt_policy policy; 1551 struct inode *inode = file_inode(filp); 1552 int err; 1553 1554 err = fscrypt_get_policy(inode, &policy); 1555 if (err) 1556 return err; 1557 1558 if (copy_to_user((struct fscrypt_policy __user *)arg, &policy, sizeof(policy))) 1559 return -EFAULT; 1560 return 0; 1561 } 1562 1563 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg) 1564 { 1565 struct inode *inode = file_inode(filp); 1566 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1567 int err; 1568 1569 if (!f2fs_sb_has_crypto(inode->i_sb)) 1570 return -EOPNOTSUPP; 1571 1572 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt)) 1573 goto got_it; 1574 1575 err = mnt_want_write_file(filp); 1576 if (err) 1577 return err; 1578 1579 /* update superblock with uuid */ 1580 generate_random_uuid(sbi->raw_super->encrypt_pw_salt); 1581 1582 err = f2fs_commit_super(sbi, false); 1583 if (err) { 1584 /* undo new data */ 1585 memset(sbi->raw_super->encrypt_pw_salt, 0, 16); 1586 mnt_drop_write_file(filp); 1587 return err; 1588 } 1589 mnt_drop_write_file(filp); 1590 got_it: 1591 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt, 1592 16)) 1593 return -EFAULT; 1594 return 0; 1595 } 1596 1597 static int f2fs_ioc_gc(struct file *filp, unsigned long arg) 1598 { 1599 struct inode *inode = file_inode(filp); 1600 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1601 __u32 sync; 1602 1603 if (!capable(CAP_SYS_ADMIN)) 1604 return -EPERM; 1605 1606 if (get_user(sync, (__u32 __user *)arg)) 1607 return -EFAULT; 1608 1609 if (f2fs_readonly(sbi->sb)) 1610 return -EROFS; 1611 1612 if (!sync) { 1613 if (!mutex_trylock(&sbi->gc_mutex)) 1614 return -EBUSY; 1615 } else { 1616 mutex_lock(&sbi->gc_mutex); 1617 } 1618 1619 return f2fs_gc(sbi, sync); 1620 } 1621 1622 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg) 1623 { 1624 struct inode *inode = file_inode(filp); 1625 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1626 1627 if (!capable(CAP_SYS_ADMIN)) 1628 return -EPERM; 1629 1630 if (f2fs_readonly(sbi->sb)) 1631 return -EROFS; 1632 1633 return f2fs_sync_fs(sbi->sb, 1); 1634 } 1635 1636 static int f2fs_defragment_range(struct f2fs_sb_info *sbi, 1637 struct file *filp, 1638 struct f2fs_defragment *range) 1639 { 1640 struct inode *inode = file_inode(filp); 1641 struct f2fs_map_blocks map = { .m_next_pgofs = NULL }; 1642 struct extent_info ei; 1643 pgoff_t pg_start, pg_end; 1644 unsigned int blk_per_seg = sbi->blocks_per_seg; 1645 unsigned int total = 0, sec_num; 1646 unsigned int pages_per_sec = sbi->segs_per_sec * blk_per_seg; 1647 block_t blk_end = 0; 1648 bool fragmented = false; 1649 int err; 1650 1651 /* if in-place-update policy is enabled, don't waste time here */ 1652 if (need_inplace_update(inode)) 1653 return -EINVAL; 1654 1655 pg_start = range->start >> PAGE_SHIFT; 1656 pg_end = (range->start + range->len) >> PAGE_SHIFT; 1657 1658 f2fs_balance_fs(sbi, true); 1659 1660 inode_lock(inode); 1661 1662 /* writeback all dirty pages in the range */ 1663 err = filemap_write_and_wait_range(inode->i_mapping, range->start, 1664 range->start + range->len - 1); 1665 if (err) 1666 goto out; 1667 1668 /* 1669 * lookup mapping info in extent cache, skip defragmenting if physical 1670 * block addresses are continuous. 1671 */ 1672 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) { 1673 if (ei.fofs + ei.len >= pg_end) 1674 goto out; 1675 } 1676 1677 map.m_lblk = pg_start; 1678 1679 /* 1680 * lookup mapping info in dnode page cache, skip defragmenting if all 1681 * physical block addresses are continuous even if there are hole(s) 1682 * in logical blocks. 1683 */ 1684 while (map.m_lblk < pg_end) { 1685 map.m_len = pg_end - map.m_lblk; 1686 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ); 1687 if (err) 1688 goto out; 1689 1690 if (!(map.m_flags & F2FS_MAP_FLAGS)) { 1691 map.m_lblk++; 1692 continue; 1693 } 1694 1695 if (blk_end && blk_end != map.m_pblk) { 1696 fragmented = true; 1697 break; 1698 } 1699 blk_end = map.m_pblk + map.m_len; 1700 1701 map.m_lblk += map.m_len; 1702 } 1703 1704 if (!fragmented) 1705 goto out; 1706 1707 map.m_lblk = pg_start; 1708 map.m_len = pg_end - pg_start; 1709 1710 sec_num = (map.m_len + pages_per_sec - 1) / pages_per_sec; 1711 1712 /* 1713 * make sure there are enough free section for LFS allocation, this can 1714 * avoid defragment running in SSR mode when free section are allocated 1715 * intensively 1716 */ 1717 if (has_not_enough_free_secs(sbi, sec_num)) { 1718 err = -EAGAIN; 1719 goto out; 1720 } 1721 1722 while (map.m_lblk < pg_end) { 1723 pgoff_t idx; 1724 int cnt = 0; 1725 1726 do_map: 1727 map.m_len = pg_end - map.m_lblk; 1728 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ); 1729 if (err) 1730 goto clear_out; 1731 1732 if (!(map.m_flags & F2FS_MAP_FLAGS)) { 1733 map.m_lblk++; 1734 continue; 1735 } 1736 1737 set_inode_flag(F2FS_I(inode), FI_DO_DEFRAG); 1738 1739 idx = map.m_lblk; 1740 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) { 1741 struct page *page; 1742 1743 page = get_lock_data_page(inode, idx, true); 1744 if (IS_ERR(page)) { 1745 err = PTR_ERR(page); 1746 goto clear_out; 1747 } 1748 1749 set_page_dirty(page); 1750 f2fs_put_page(page, 1); 1751 1752 idx++; 1753 cnt++; 1754 total++; 1755 } 1756 1757 map.m_lblk = idx; 1758 1759 if (idx < pg_end && cnt < blk_per_seg) 1760 goto do_map; 1761 1762 clear_inode_flag(F2FS_I(inode), FI_DO_DEFRAG); 1763 1764 err = filemap_fdatawrite(inode->i_mapping); 1765 if (err) 1766 goto out; 1767 } 1768 clear_out: 1769 clear_inode_flag(F2FS_I(inode), FI_DO_DEFRAG); 1770 out: 1771 inode_unlock(inode); 1772 if (!err) 1773 range->len = (u64)total << PAGE_SHIFT; 1774 return err; 1775 } 1776 1777 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg) 1778 { 1779 struct inode *inode = file_inode(filp); 1780 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1781 struct f2fs_defragment range; 1782 int err; 1783 1784 if (!capable(CAP_SYS_ADMIN)) 1785 return -EPERM; 1786 1787 if (!S_ISREG(inode->i_mode)) 1788 return -EINVAL; 1789 1790 err = mnt_want_write_file(filp); 1791 if (err) 1792 return err; 1793 1794 if (f2fs_readonly(sbi->sb)) { 1795 err = -EROFS; 1796 goto out; 1797 } 1798 1799 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg, 1800 sizeof(range))) { 1801 err = -EFAULT; 1802 goto out; 1803 } 1804 1805 /* verify alignment of offset & size */ 1806 if (range.start & (F2FS_BLKSIZE - 1) || 1807 range.len & (F2FS_BLKSIZE - 1)) { 1808 err = -EINVAL; 1809 goto out; 1810 } 1811 1812 err = f2fs_defragment_range(sbi, filp, &range); 1813 f2fs_update_time(sbi, REQ_TIME); 1814 if (err < 0) 1815 goto out; 1816 1817 if (copy_to_user((struct f2fs_defragment __user *)arg, &range, 1818 sizeof(range))) 1819 err = -EFAULT; 1820 out: 1821 mnt_drop_write_file(filp); 1822 return err; 1823 } 1824 1825 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1826 { 1827 switch (cmd) { 1828 case F2FS_IOC_GETFLAGS: 1829 return f2fs_ioc_getflags(filp, arg); 1830 case F2FS_IOC_SETFLAGS: 1831 return f2fs_ioc_setflags(filp, arg); 1832 case F2FS_IOC_GETVERSION: 1833 return f2fs_ioc_getversion(filp, arg); 1834 case F2FS_IOC_START_ATOMIC_WRITE: 1835 return f2fs_ioc_start_atomic_write(filp); 1836 case F2FS_IOC_COMMIT_ATOMIC_WRITE: 1837 return f2fs_ioc_commit_atomic_write(filp); 1838 case F2FS_IOC_START_VOLATILE_WRITE: 1839 return f2fs_ioc_start_volatile_write(filp); 1840 case F2FS_IOC_RELEASE_VOLATILE_WRITE: 1841 return f2fs_ioc_release_volatile_write(filp); 1842 case F2FS_IOC_ABORT_VOLATILE_WRITE: 1843 return f2fs_ioc_abort_volatile_write(filp); 1844 case F2FS_IOC_SHUTDOWN: 1845 return f2fs_ioc_shutdown(filp, arg); 1846 case FITRIM: 1847 return f2fs_ioc_fitrim(filp, arg); 1848 case F2FS_IOC_SET_ENCRYPTION_POLICY: 1849 return f2fs_ioc_set_encryption_policy(filp, arg); 1850 case F2FS_IOC_GET_ENCRYPTION_POLICY: 1851 return f2fs_ioc_get_encryption_policy(filp, arg); 1852 case F2FS_IOC_GET_ENCRYPTION_PWSALT: 1853 return f2fs_ioc_get_encryption_pwsalt(filp, arg); 1854 case F2FS_IOC_GARBAGE_COLLECT: 1855 return f2fs_ioc_gc(filp, arg); 1856 case F2FS_IOC_WRITE_CHECKPOINT: 1857 return f2fs_ioc_write_checkpoint(filp, arg); 1858 case F2FS_IOC_DEFRAGMENT: 1859 return f2fs_ioc_defragment(filp, arg); 1860 default: 1861 return -ENOTTY; 1862 } 1863 } 1864 1865 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 1866 { 1867 struct file *file = iocb->ki_filp; 1868 struct inode *inode = file_inode(file); 1869 ssize_t ret; 1870 1871 if (f2fs_encrypted_inode(inode) && 1872 !fscrypt_has_encryption_key(inode) && 1873 fscrypt_get_encryption_info(inode)) 1874 return -EACCES; 1875 1876 inode_lock(inode); 1877 ret = generic_write_checks(iocb, from); 1878 if (ret > 0) { 1879 ret = f2fs_preallocate_blocks(iocb, from); 1880 if (!ret) 1881 ret = __generic_file_write_iter(iocb, from); 1882 } 1883 inode_unlock(inode); 1884 1885 if (ret > 0) { 1886 ssize_t err; 1887 1888 err = generic_write_sync(file, iocb->ki_pos - ret, ret); 1889 if (err < 0) 1890 ret = err; 1891 } 1892 return ret; 1893 } 1894 1895 #ifdef CONFIG_COMPAT 1896 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 1897 { 1898 switch (cmd) { 1899 case F2FS_IOC32_GETFLAGS: 1900 cmd = F2FS_IOC_GETFLAGS; 1901 break; 1902 case F2FS_IOC32_SETFLAGS: 1903 cmd = F2FS_IOC_SETFLAGS; 1904 break; 1905 case F2FS_IOC32_GETVERSION: 1906 cmd = F2FS_IOC_GETVERSION; 1907 break; 1908 case F2FS_IOC_START_ATOMIC_WRITE: 1909 case F2FS_IOC_COMMIT_ATOMIC_WRITE: 1910 case F2FS_IOC_START_VOLATILE_WRITE: 1911 case F2FS_IOC_RELEASE_VOLATILE_WRITE: 1912 case F2FS_IOC_ABORT_VOLATILE_WRITE: 1913 case F2FS_IOC_SHUTDOWN: 1914 case F2FS_IOC_SET_ENCRYPTION_POLICY: 1915 case F2FS_IOC_GET_ENCRYPTION_PWSALT: 1916 case F2FS_IOC_GET_ENCRYPTION_POLICY: 1917 case F2FS_IOC_GARBAGE_COLLECT: 1918 case F2FS_IOC_WRITE_CHECKPOINT: 1919 case F2FS_IOC_DEFRAGMENT: 1920 break; 1921 default: 1922 return -ENOIOCTLCMD; 1923 } 1924 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 1925 } 1926 #endif 1927 1928 const struct file_operations f2fs_file_operations = { 1929 .llseek = f2fs_llseek, 1930 .read_iter = generic_file_read_iter, 1931 .write_iter = f2fs_file_write_iter, 1932 .open = f2fs_file_open, 1933 .release = f2fs_release_file, 1934 .mmap = f2fs_file_mmap, 1935 .fsync = f2fs_sync_file, 1936 .fallocate = f2fs_fallocate, 1937 .unlocked_ioctl = f2fs_ioctl, 1938 #ifdef CONFIG_COMPAT 1939 .compat_ioctl = f2fs_compat_ioctl, 1940 #endif 1941 .splice_read = generic_file_splice_read, 1942 .splice_write = iter_file_splice_write, 1943 }; 1944