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/uio.h> 24 #include <linux/uuid.h> 25 #include <linux/file.h> 26 27 #include "f2fs.h" 28 #include "node.h" 29 #include "segment.h" 30 #include "xattr.h" 31 #include "acl.h" 32 #include "gc.h" 33 #include "trace.h" 34 #include <trace/events/f2fs.h> 35 36 static int f2fs_filemap_fault(struct vm_fault *vmf) 37 { 38 struct inode *inode = file_inode(vmf->vma->vm_file); 39 int err; 40 41 down_read(&F2FS_I(inode)->i_mmap_sem); 42 err = filemap_fault(vmf); 43 up_read(&F2FS_I(inode)->i_mmap_sem); 44 45 return err; 46 } 47 48 static int 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 int err; 55 56 sb_start_pagefault(inode->i_sb); 57 58 f2fs_bug_on(sbi, f2fs_has_inline_data(inode)); 59 60 /* block allocation */ 61 f2fs_lock_op(sbi); 62 set_new_dnode(&dn, inode, NULL, NULL, 0); 63 err = f2fs_reserve_block(&dn, page->index); 64 if (err) { 65 f2fs_unlock_op(sbi); 66 goto out; 67 } 68 f2fs_put_dnode(&dn); 69 f2fs_unlock_op(sbi); 70 71 f2fs_balance_fs(sbi, dn.node_changed); 72 73 file_update_time(vmf->vma->vm_file); 74 down_read(&F2FS_I(inode)->i_mmap_sem); 75 lock_page(page); 76 if (unlikely(page->mapping != inode->i_mapping || 77 page_offset(page) > i_size_read(inode) || 78 !PageUptodate(page))) { 79 unlock_page(page); 80 err = -EFAULT; 81 goto out_sem; 82 } 83 84 /* 85 * check to see if the page is mapped already (no holes) 86 */ 87 if (PageMappedToDisk(page)) 88 goto mapped; 89 90 /* page is wholly or partially inside EOF */ 91 if (((loff_t)(page->index + 1) << PAGE_SHIFT) > 92 i_size_read(inode)) { 93 unsigned offset; 94 offset = i_size_read(inode) & ~PAGE_MASK; 95 zero_user_segment(page, offset, PAGE_SIZE); 96 } 97 set_page_dirty(page); 98 if (!PageUptodate(page)) 99 SetPageUptodate(page); 100 101 trace_f2fs_vm_page_mkwrite(page, DATA); 102 mapped: 103 /* fill the page */ 104 f2fs_wait_on_page_writeback(page, DATA, false); 105 106 /* wait for GCed encrypted page writeback */ 107 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) 108 f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr); 109 110 out_sem: 111 up_read(&F2FS_I(inode)->i_mmap_sem); 112 out: 113 sb_end_pagefault(inode->i_sb); 114 f2fs_update_time(sbi, REQ_TIME); 115 return block_page_mkwrite_return(err); 116 } 117 118 static const struct vm_operations_struct f2fs_file_vm_ops = { 119 .fault = f2fs_filemap_fault, 120 .map_pages = filemap_map_pages, 121 .page_mkwrite = f2fs_vm_page_mkwrite, 122 }; 123 124 static int get_parent_ino(struct inode *inode, nid_t *pino) 125 { 126 struct dentry *dentry; 127 128 inode = igrab(inode); 129 dentry = d_find_any_alias(inode); 130 iput(inode); 131 if (!dentry) 132 return 0; 133 134 *pino = parent_ino(dentry); 135 dput(dentry); 136 return 1; 137 } 138 139 static inline bool need_do_checkpoint(struct inode *inode) 140 { 141 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 142 bool need_cp = false; 143 144 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1) 145 need_cp = true; 146 else if (is_sbi_flag_set(sbi, SBI_NEED_CP)) 147 need_cp = true; 148 else if (file_wrong_pino(inode)) 149 need_cp = true; 150 else if (!space_for_roll_forward(sbi)) 151 need_cp = true; 152 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino)) 153 need_cp = true; 154 else if (test_opt(sbi, FASTBOOT)) 155 need_cp = true; 156 else if (sbi->active_logs == 2) 157 need_cp = true; 158 159 return need_cp; 160 } 161 162 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino) 163 { 164 struct page *i = find_get_page(NODE_MAPPING(sbi), ino); 165 bool ret = false; 166 /* But we need to avoid that there are some inode updates */ 167 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino)) 168 ret = true; 169 f2fs_put_page(i, 0); 170 return ret; 171 } 172 173 static void try_to_fix_pino(struct inode *inode) 174 { 175 struct f2fs_inode_info *fi = F2FS_I(inode); 176 nid_t pino; 177 178 down_write(&fi->i_sem); 179 if (file_wrong_pino(inode) && inode->i_nlink == 1 && 180 get_parent_ino(inode, &pino)) { 181 f2fs_i_pino_write(inode, pino); 182 file_got_pino(inode); 183 } 184 up_write(&fi->i_sem); 185 } 186 187 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end, 188 int datasync, bool atomic) 189 { 190 struct inode *inode = file->f_mapping->host; 191 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 192 nid_t ino = inode->i_ino; 193 int ret = 0; 194 bool need_cp = false; 195 struct writeback_control wbc = { 196 .sync_mode = WB_SYNC_ALL, 197 .nr_to_write = LONG_MAX, 198 .for_reclaim = 0, 199 }; 200 201 if (unlikely(f2fs_readonly(inode->i_sb))) 202 return 0; 203 204 trace_f2fs_sync_file_enter(inode); 205 206 /* if fdatasync is triggered, let's do in-place-update */ 207 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks) 208 set_inode_flag(inode, FI_NEED_IPU); 209 ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 210 clear_inode_flag(inode, FI_NEED_IPU); 211 212 if (ret) { 213 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); 214 return ret; 215 } 216 217 /* if the inode is dirty, let's recover all the time */ 218 if (!f2fs_skip_inode_update(inode, datasync)) { 219 f2fs_write_inode(inode, NULL); 220 goto go_write; 221 } 222 223 /* 224 * if there is no written data, don't waste time to write recovery info. 225 */ 226 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) && 227 !exist_written_data(sbi, ino, APPEND_INO)) { 228 229 /* it may call write_inode just prior to fsync */ 230 if (need_inode_page_update(sbi, ino)) 231 goto go_write; 232 233 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) || 234 exist_written_data(sbi, ino, UPDATE_INO)) 235 goto flush_out; 236 goto out; 237 } 238 go_write: 239 /* 240 * Both of fdatasync() and fsync() are able to be recovered from 241 * sudden-power-off. 242 */ 243 down_read(&F2FS_I(inode)->i_sem); 244 need_cp = need_do_checkpoint(inode); 245 up_read(&F2FS_I(inode)->i_sem); 246 247 if (need_cp) { 248 /* all the dirty node pages should be flushed for POR */ 249 ret = f2fs_sync_fs(inode->i_sb, 1); 250 251 /* 252 * We've secured consistency through sync_fs. Following pino 253 * will be used only for fsynced inodes after checkpoint. 254 */ 255 try_to_fix_pino(inode); 256 clear_inode_flag(inode, FI_APPEND_WRITE); 257 clear_inode_flag(inode, FI_UPDATE_WRITE); 258 goto out; 259 } 260 sync_nodes: 261 ret = fsync_node_pages(sbi, inode, &wbc, atomic); 262 if (ret) 263 goto out; 264 265 /* if cp_error was enabled, we should avoid infinite loop */ 266 if (unlikely(f2fs_cp_error(sbi))) { 267 ret = -EIO; 268 goto out; 269 } 270 271 if (need_inode_block_update(sbi, ino)) { 272 f2fs_mark_inode_dirty_sync(inode, true); 273 f2fs_write_inode(inode, NULL); 274 goto sync_nodes; 275 } 276 277 ret = wait_on_node_pages_writeback(sbi, ino); 278 if (ret) 279 goto out; 280 281 /* once recovery info is written, don't need to tack this */ 282 remove_ino_entry(sbi, ino, APPEND_INO); 283 clear_inode_flag(inode, FI_APPEND_WRITE); 284 flush_out: 285 remove_ino_entry(sbi, ino, UPDATE_INO); 286 clear_inode_flag(inode, FI_UPDATE_WRITE); 287 if (!atomic) 288 ret = f2fs_issue_flush(sbi); 289 f2fs_update_time(sbi, REQ_TIME); 290 out: 291 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); 292 f2fs_trace_ios(NULL, 1); 293 return ret; 294 } 295 296 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) 297 { 298 return f2fs_do_sync_file(file, start, end, datasync, false); 299 } 300 301 static pgoff_t __get_first_dirty_index(struct address_space *mapping, 302 pgoff_t pgofs, int whence) 303 { 304 struct pagevec pvec; 305 int nr_pages; 306 307 if (whence != SEEK_DATA) 308 return 0; 309 310 /* find first dirty page index */ 311 pagevec_init(&pvec, 0); 312 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs, 313 PAGECACHE_TAG_DIRTY, 1); 314 pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX; 315 pagevec_release(&pvec); 316 return pgofs; 317 } 318 319 static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs, 320 int whence) 321 { 322 switch (whence) { 323 case SEEK_DATA: 324 if ((blkaddr == NEW_ADDR && dirty == pgofs) || 325 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR)) 326 return true; 327 break; 328 case SEEK_HOLE: 329 if (blkaddr == NULL_ADDR) 330 return true; 331 break; 332 } 333 return false; 334 } 335 336 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) 337 { 338 struct inode *inode = file->f_mapping->host; 339 loff_t maxbytes = inode->i_sb->s_maxbytes; 340 struct dnode_of_data dn; 341 pgoff_t pgofs, end_offset, dirty; 342 loff_t data_ofs = offset; 343 loff_t isize; 344 int err = 0; 345 346 inode_lock(inode); 347 348 isize = i_size_read(inode); 349 if (offset >= isize) 350 goto fail; 351 352 /* handle inline data case */ 353 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) { 354 if (whence == SEEK_HOLE) 355 data_ofs = isize; 356 goto found; 357 } 358 359 pgofs = (pgoff_t)(offset >> PAGE_SHIFT); 360 361 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence); 362 363 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) { 364 set_new_dnode(&dn, inode, NULL, NULL, 0); 365 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE); 366 if (err && err != -ENOENT) { 367 goto fail; 368 } else if (err == -ENOENT) { 369 /* direct node does not exists */ 370 if (whence == SEEK_DATA) { 371 pgofs = get_next_page_offset(&dn, pgofs); 372 continue; 373 } else { 374 goto found; 375 } 376 } 377 378 end_offset = ADDRS_PER_PAGE(dn.node_page, inode); 379 380 /* find data/hole in dnode block */ 381 for (; dn.ofs_in_node < end_offset; 382 dn.ofs_in_node++, pgofs++, 383 data_ofs = (loff_t)pgofs << PAGE_SHIFT) { 384 block_t blkaddr; 385 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); 386 387 if (__found_offset(blkaddr, dirty, pgofs, whence)) { 388 f2fs_put_dnode(&dn); 389 goto found; 390 } 391 } 392 f2fs_put_dnode(&dn); 393 } 394 395 if (whence == SEEK_DATA) 396 goto fail; 397 found: 398 if (whence == SEEK_HOLE && data_ofs > isize) 399 data_ofs = isize; 400 inode_unlock(inode); 401 return vfs_setpos(file, data_ofs, maxbytes); 402 fail: 403 inode_unlock(inode); 404 return -ENXIO; 405 } 406 407 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence) 408 { 409 struct inode *inode = file->f_mapping->host; 410 loff_t maxbytes = inode->i_sb->s_maxbytes; 411 412 switch (whence) { 413 case SEEK_SET: 414 case SEEK_CUR: 415 case SEEK_END: 416 return generic_file_llseek_size(file, offset, whence, 417 maxbytes, i_size_read(inode)); 418 case SEEK_DATA: 419 case SEEK_HOLE: 420 if (offset < 0) 421 return -ENXIO; 422 return f2fs_seek_block(file, offset, whence); 423 } 424 425 return -EINVAL; 426 } 427 428 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) 429 { 430 struct inode *inode = file_inode(file); 431 int err; 432 433 /* we don't need to use inline_data strictly */ 434 err = f2fs_convert_inline_inode(inode); 435 if (err) 436 return err; 437 438 file_accessed(file); 439 vma->vm_ops = &f2fs_file_vm_ops; 440 return 0; 441 } 442 443 static int f2fs_file_open(struct inode *inode, struct file *filp) 444 { 445 struct dentry *dir; 446 447 if (f2fs_encrypted_inode(inode)) { 448 int ret = fscrypt_get_encryption_info(inode); 449 if (ret) 450 return -EACCES; 451 if (!fscrypt_has_encryption_key(inode)) 452 return -ENOKEY; 453 } 454 dir = dget_parent(file_dentry(filp)); 455 if (f2fs_encrypted_inode(d_inode(dir)) && 456 !fscrypt_has_permitted_context(d_inode(dir), inode)) { 457 dput(dir); 458 return -EPERM; 459 } 460 dput(dir); 461 return dquot_file_open(inode, filp); 462 } 463 464 int truncate_data_blocks_range(struct dnode_of_data *dn, int count) 465 { 466 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 467 struct f2fs_node *raw_node; 468 int nr_free = 0, ofs = dn->ofs_in_node, len = count; 469 __le32 *addr; 470 471 raw_node = F2FS_NODE(dn->node_page); 472 addr = blkaddr_in_node(raw_node) + ofs; 473 474 for (; count > 0; count--, addr++, dn->ofs_in_node++) { 475 block_t blkaddr = le32_to_cpu(*addr); 476 if (blkaddr == NULL_ADDR) 477 continue; 478 479 dn->data_blkaddr = NULL_ADDR; 480 set_data_blkaddr(dn); 481 invalidate_blocks(sbi, blkaddr); 482 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page)) 483 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN); 484 nr_free++; 485 } 486 487 if (nr_free) { 488 pgoff_t fofs; 489 /* 490 * once we invalidate valid blkaddr in range [ofs, ofs + count], 491 * we will invalidate all blkaddr in the whole range. 492 */ 493 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), 494 dn->inode) + ofs; 495 f2fs_update_extent_cache_range(dn, fofs, 0, len); 496 dec_valid_block_count(sbi, dn->inode, nr_free); 497 } 498 dn->ofs_in_node = ofs; 499 500 f2fs_update_time(sbi, REQ_TIME); 501 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid, 502 dn->ofs_in_node, nr_free); 503 return nr_free; 504 } 505 506 void truncate_data_blocks(struct dnode_of_data *dn) 507 { 508 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK); 509 } 510 511 static int truncate_partial_data_page(struct inode *inode, u64 from, 512 bool cache_only) 513 { 514 unsigned offset = from & (PAGE_SIZE - 1); 515 pgoff_t index = from >> PAGE_SHIFT; 516 struct address_space *mapping = inode->i_mapping; 517 struct page *page; 518 519 if (!offset && !cache_only) 520 return 0; 521 522 if (cache_only) { 523 page = find_lock_page(mapping, index); 524 if (page && PageUptodate(page)) 525 goto truncate_out; 526 f2fs_put_page(page, 1); 527 return 0; 528 } 529 530 page = get_lock_data_page(inode, index, true); 531 if (IS_ERR(page)) 532 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page); 533 truncate_out: 534 f2fs_wait_on_page_writeback(page, DATA, true); 535 zero_user(page, offset, PAGE_SIZE - offset); 536 537 /* An encrypted inode should have a key and truncate the last page. */ 538 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode)); 539 if (!cache_only) 540 set_page_dirty(page); 541 f2fs_put_page(page, 1); 542 return 0; 543 } 544 545 int truncate_blocks(struct inode *inode, u64 from, bool lock) 546 { 547 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 548 unsigned int blocksize = inode->i_sb->s_blocksize; 549 struct dnode_of_data dn; 550 pgoff_t free_from; 551 int count = 0, err = 0; 552 struct page *ipage; 553 bool truncate_page = false; 554 555 trace_f2fs_truncate_blocks_enter(inode, from); 556 557 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1); 558 559 if (free_from >= sbi->max_file_blocks) 560 goto free_partial; 561 562 if (lock) 563 f2fs_lock_op(sbi); 564 565 ipage = get_node_page(sbi, inode->i_ino); 566 if (IS_ERR(ipage)) { 567 err = PTR_ERR(ipage); 568 goto out; 569 } 570 571 if (f2fs_has_inline_data(inode)) { 572 truncate_inline_inode(inode, ipage, from); 573 f2fs_put_page(ipage, 1); 574 truncate_page = true; 575 goto out; 576 } 577 578 set_new_dnode(&dn, inode, ipage, NULL, 0); 579 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA); 580 if (err) { 581 if (err == -ENOENT) 582 goto free_next; 583 goto out; 584 } 585 586 count = ADDRS_PER_PAGE(dn.node_page, inode); 587 588 count -= dn.ofs_in_node; 589 f2fs_bug_on(sbi, count < 0); 590 591 if (dn.ofs_in_node || IS_INODE(dn.node_page)) { 592 truncate_data_blocks_range(&dn, count); 593 free_from += count; 594 } 595 596 f2fs_put_dnode(&dn); 597 free_next: 598 err = truncate_inode_blocks(inode, free_from); 599 out: 600 if (lock) 601 f2fs_unlock_op(sbi); 602 free_partial: 603 /* lastly zero out the first data page */ 604 if (!err) 605 err = truncate_partial_data_page(inode, from, truncate_page); 606 607 trace_f2fs_truncate_blocks_exit(inode, err); 608 return err; 609 } 610 611 int f2fs_truncate(struct inode *inode) 612 { 613 int err; 614 615 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 616 S_ISLNK(inode->i_mode))) 617 return 0; 618 619 trace_f2fs_truncate(inode); 620 621 #ifdef CONFIG_F2FS_FAULT_INJECTION 622 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) { 623 f2fs_show_injection_info(FAULT_TRUNCATE); 624 return -EIO; 625 } 626 #endif 627 /* we should check inline_data size */ 628 if (!f2fs_may_inline_data(inode)) { 629 err = f2fs_convert_inline_inode(inode); 630 if (err) 631 return err; 632 } 633 634 err = truncate_blocks(inode, i_size_read(inode), true); 635 if (err) 636 return err; 637 638 inode->i_mtime = inode->i_ctime = current_time(inode); 639 f2fs_mark_inode_dirty_sync(inode, false); 640 return 0; 641 } 642 643 int f2fs_getattr(const struct path *path, struct kstat *stat, 644 u32 request_mask, unsigned int query_flags) 645 { 646 struct inode *inode = d_inode(path->dentry); 647 struct f2fs_inode_info *fi = F2FS_I(inode); 648 unsigned int flags; 649 650 flags = fi->i_flags & FS_FL_USER_VISIBLE; 651 if (flags & FS_APPEND_FL) 652 stat->attributes |= STATX_ATTR_APPEND; 653 if (flags & FS_COMPR_FL) 654 stat->attributes |= STATX_ATTR_COMPRESSED; 655 if (f2fs_encrypted_inode(inode)) 656 stat->attributes |= STATX_ATTR_ENCRYPTED; 657 if (flags & FS_IMMUTABLE_FL) 658 stat->attributes |= STATX_ATTR_IMMUTABLE; 659 if (flags & FS_NODUMP_FL) 660 stat->attributes |= STATX_ATTR_NODUMP; 661 662 stat->attributes_mask |= (STATX_ATTR_APPEND | 663 STATX_ATTR_COMPRESSED | 664 STATX_ATTR_ENCRYPTED | 665 STATX_ATTR_IMMUTABLE | 666 STATX_ATTR_NODUMP); 667 668 generic_fillattr(inode, stat); 669 return 0; 670 } 671 672 #ifdef CONFIG_F2FS_FS_POSIX_ACL 673 static void __setattr_copy(struct inode *inode, const struct iattr *attr) 674 { 675 unsigned int ia_valid = attr->ia_valid; 676 677 if (ia_valid & ATTR_UID) 678 inode->i_uid = attr->ia_uid; 679 if (ia_valid & ATTR_GID) 680 inode->i_gid = attr->ia_gid; 681 if (ia_valid & ATTR_ATIME) 682 inode->i_atime = timespec_trunc(attr->ia_atime, 683 inode->i_sb->s_time_gran); 684 if (ia_valid & ATTR_MTIME) 685 inode->i_mtime = timespec_trunc(attr->ia_mtime, 686 inode->i_sb->s_time_gran); 687 if (ia_valid & ATTR_CTIME) 688 inode->i_ctime = timespec_trunc(attr->ia_ctime, 689 inode->i_sb->s_time_gran); 690 if (ia_valid & ATTR_MODE) { 691 umode_t mode = attr->ia_mode; 692 693 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) 694 mode &= ~S_ISGID; 695 set_acl_inode(inode, mode); 696 } 697 } 698 #else 699 #define __setattr_copy setattr_copy 700 #endif 701 702 int f2fs_setattr(struct dentry *dentry, struct iattr *attr) 703 { 704 struct inode *inode = d_inode(dentry); 705 int err; 706 bool size_changed = false; 707 708 err = setattr_prepare(dentry, attr); 709 if (err) 710 return err; 711 712 if (is_quota_modification(inode, attr)) { 713 err = dquot_initialize(inode); 714 if (err) 715 return err; 716 } 717 if ((attr->ia_valid & ATTR_UID && 718 !uid_eq(attr->ia_uid, inode->i_uid)) || 719 (attr->ia_valid & ATTR_GID && 720 !gid_eq(attr->ia_gid, inode->i_gid))) { 721 err = dquot_transfer(inode, attr); 722 if (err) 723 return err; 724 } 725 726 if (attr->ia_valid & ATTR_SIZE) { 727 if (f2fs_encrypted_inode(inode)) { 728 err = fscrypt_get_encryption_info(inode); 729 if (err) 730 return err; 731 if (!fscrypt_has_encryption_key(inode)) 732 return -ENOKEY; 733 } 734 735 if (attr->ia_size <= i_size_read(inode)) { 736 down_write(&F2FS_I(inode)->i_mmap_sem); 737 truncate_setsize(inode, attr->ia_size); 738 err = f2fs_truncate(inode); 739 up_write(&F2FS_I(inode)->i_mmap_sem); 740 if (err) 741 return err; 742 } else { 743 /* 744 * do not trim all blocks after i_size if target size is 745 * larger than i_size. 746 */ 747 down_write(&F2FS_I(inode)->i_mmap_sem); 748 truncate_setsize(inode, attr->ia_size); 749 up_write(&F2FS_I(inode)->i_mmap_sem); 750 751 /* should convert inline inode here */ 752 if (!f2fs_may_inline_data(inode)) { 753 err = f2fs_convert_inline_inode(inode); 754 if (err) 755 return err; 756 } 757 inode->i_mtime = inode->i_ctime = current_time(inode); 758 } 759 760 size_changed = true; 761 } 762 763 __setattr_copy(inode, attr); 764 765 if (attr->ia_valid & ATTR_MODE) { 766 err = posix_acl_chmod(inode, get_inode_mode(inode)); 767 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) { 768 inode->i_mode = F2FS_I(inode)->i_acl_mode; 769 clear_inode_flag(inode, FI_ACL_MODE); 770 } 771 } 772 773 /* file size may changed here */ 774 f2fs_mark_inode_dirty_sync(inode, size_changed); 775 776 /* inode change will produce dirty node pages flushed by checkpoint */ 777 f2fs_balance_fs(F2FS_I_SB(inode), true); 778 779 return err; 780 } 781 782 const struct inode_operations f2fs_file_inode_operations = { 783 .getattr = f2fs_getattr, 784 .setattr = f2fs_setattr, 785 .get_acl = f2fs_get_acl, 786 .set_acl = f2fs_set_acl, 787 #ifdef CONFIG_F2FS_FS_XATTR 788 .listxattr = f2fs_listxattr, 789 #endif 790 .fiemap = f2fs_fiemap, 791 }; 792 793 static int fill_zero(struct inode *inode, pgoff_t index, 794 loff_t start, loff_t len) 795 { 796 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 797 struct page *page; 798 799 if (!len) 800 return 0; 801 802 f2fs_balance_fs(sbi, true); 803 804 f2fs_lock_op(sbi); 805 page = get_new_data_page(inode, NULL, index, false); 806 f2fs_unlock_op(sbi); 807 808 if (IS_ERR(page)) 809 return PTR_ERR(page); 810 811 f2fs_wait_on_page_writeback(page, DATA, true); 812 zero_user(page, start, len); 813 set_page_dirty(page); 814 f2fs_put_page(page, 1); 815 return 0; 816 } 817 818 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) 819 { 820 int err; 821 822 while (pg_start < pg_end) { 823 struct dnode_of_data dn; 824 pgoff_t end_offset, count; 825 826 set_new_dnode(&dn, inode, NULL, NULL, 0); 827 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE); 828 if (err) { 829 if (err == -ENOENT) { 830 pg_start++; 831 continue; 832 } 833 return err; 834 } 835 836 end_offset = ADDRS_PER_PAGE(dn.node_page, inode); 837 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start); 838 839 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset); 840 841 truncate_data_blocks_range(&dn, count); 842 f2fs_put_dnode(&dn); 843 844 pg_start += count; 845 } 846 return 0; 847 } 848 849 static int punch_hole(struct inode *inode, loff_t offset, loff_t len) 850 { 851 pgoff_t pg_start, pg_end; 852 loff_t off_start, off_end; 853 int ret; 854 855 ret = f2fs_convert_inline_inode(inode); 856 if (ret) 857 return ret; 858 859 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT; 860 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT; 861 862 off_start = offset & (PAGE_SIZE - 1); 863 off_end = (offset + len) & (PAGE_SIZE - 1); 864 865 if (pg_start == pg_end) { 866 ret = fill_zero(inode, pg_start, off_start, 867 off_end - off_start); 868 if (ret) 869 return ret; 870 } else { 871 if (off_start) { 872 ret = fill_zero(inode, pg_start++, off_start, 873 PAGE_SIZE - off_start); 874 if (ret) 875 return ret; 876 } 877 if (off_end) { 878 ret = fill_zero(inode, pg_end, 0, off_end); 879 if (ret) 880 return ret; 881 } 882 883 if (pg_start < pg_end) { 884 struct address_space *mapping = inode->i_mapping; 885 loff_t blk_start, blk_end; 886 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 887 888 f2fs_balance_fs(sbi, true); 889 890 blk_start = (loff_t)pg_start << PAGE_SHIFT; 891 blk_end = (loff_t)pg_end << PAGE_SHIFT; 892 down_write(&F2FS_I(inode)->i_mmap_sem); 893 truncate_inode_pages_range(mapping, blk_start, 894 blk_end - 1); 895 896 f2fs_lock_op(sbi); 897 ret = truncate_hole(inode, pg_start, pg_end); 898 f2fs_unlock_op(sbi); 899 up_write(&F2FS_I(inode)->i_mmap_sem); 900 } 901 } 902 903 return ret; 904 } 905 906 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr, 907 int *do_replace, pgoff_t off, pgoff_t len) 908 { 909 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 910 struct dnode_of_data dn; 911 int ret, done, i; 912 913 next_dnode: 914 set_new_dnode(&dn, inode, NULL, NULL, 0); 915 ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA); 916 if (ret && ret != -ENOENT) { 917 return ret; 918 } else if (ret == -ENOENT) { 919 if (dn.max_level == 0) 920 return -ENOENT; 921 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len); 922 blkaddr += done; 923 do_replace += done; 924 goto next; 925 } 926 927 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) - 928 dn.ofs_in_node, len); 929 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) { 930 *blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); 931 if (!is_checkpointed_data(sbi, *blkaddr)) { 932 933 if (test_opt(sbi, LFS)) { 934 f2fs_put_dnode(&dn); 935 return -ENOTSUPP; 936 } 937 938 /* do not invalidate this block address */ 939 f2fs_update_data_blkaddr(&dn, NULL_ADDR); 940 *do_replace = 1; 941 } 942 } 943 f2fs_put_dnode(&dn); 944 next: 945 len -= done; 946 off += done; 947 if (len) 948 goto next_dnode; 949 return 0; 950 } 951 952 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr, 953 int *do_replace, pgoff_t off, int len) 954 { 955 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 956 struct dnode_of_data dn; 957 int ret, i; 958 959 for (i = 0; i < len; i++, do_replace++, blkaddr++) { 960 if (*do_replace == 0) 961 continue; 962 963 set_new_dnode(&dn, inode, NULL, NULL, 0); 964 ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA); 965 if (ret) { 966 dec_valid_block_count(sbi, inode, 1); 967 invalidate_blocks(sbi, *blkaddr); 968 } else { 969 f2fs_update_data_blkaddr(&dn, *blkaddr); 970 } 971 f2fs_put_dnode(&dn); 972 } 973 return 0; 974 } 975 976 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode, 977 block_t *blkaddr, int *do_replace, 978 pgoff_t src, pgoff_t dst, pgoff_t len, bool full) 979 { 980 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode); 981 pgoff_t i = 0; 982 int ret; 983 984 while (i < len) { 985 if (blkaddr[i] == NULL_ADDR && !full) { 986 i++; 987 continue; 988 } 989 990 if (do_replace[i] || blkaddr[i] == NULL_ADDR) { 991 struct dnode_of_data dn; 992 struct node_info ni; 993 size_t new_size; 994 pgoff_t ilen; 995 996 set_new_dnode(&dn, dst_inode, NULL, NULL, 0); 997 ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE); 998 if (ret) 999 return ret; 1000 1001 get_node_info(sbi, dn.nid, &ni); 1002 ilen = min((pgoff_t) 1003 ADDRS_PER_PAGE(dn.node_page, dst_inode) - 1004 dn.ofs_in_node, len - i); 1005 do { 1006 dn.data_blkaddr = datablock_addr(dn.node_page, 1007 dn.ofs_in_node); 1008 truncate_data_blocks_range(&dn, 1); 1009 1010 if (do_replace[i]) { 1011 f2fs_i_blocks_write(src_inode, 1012 1, false, false); 1013 f2fs_i_blocks_write(dst_inode, 1014 1, true, false); 1015 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, 1016 blkaddr[i], ni.version, true, false); 1017 1018 do_replace[i] = 0; 1019 } 1020 dn.ofs_in_node++; 1021 i++; 1022 new_size = (dst + i) << PAGE_SHIFT; 1023 if (dst_inode->i_size < new_size) 1024 f2fs_i_size_write(dst_inode, new_size); 1025 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR)); 1026 1027 f2fs_put_dnode(&dn); 1028 } else { 1029 struct page *psrc, *pdst; 1030 1031 psrc = get_lock_data_page(src_inode, src + i, true); 1032 if (IS_ERR(psrc)) 1033 return PTR_ERR(psrc); 1034 pdst = get_new_data_page(dst_inode, NULL, dst + i, 1035 true); 1036 if (IS_ERR(pdst)) { 1037 f2fs_put_page(psrc, 1); 1038 return PTR_ERR(pdst); 1039 } 1040 f2fs_copy_page(psrc, pdst); 1041 set_page_dirty(pdst); 1042 f2fs_put_page(pdst, 1); 1043 f2fs_put_page(psrc, 1); 1044 1045 ret = truncate_hole(src_inode, src + i, src + i + 1); 1046 if (ret) 1047 return ret; 1048 i++; 1049 } 1050 } 1051 return 0; 1052 } 1053 1054 static int __exchange_data_block(struct inode *src_inode, 1055 struct inode *dst_inode, pgoff_t src, pgoff_t dst, 1056 pgoff_t len, bool full) 1057 { 1058 block_t *src_blkaddr; 1059 int *do_replace; 1060 pgoff_t olen; 1061 int ret; 1062 1063 while (len) { 1064 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len); 1065 1066 src_blkaddr = kvzalloc(sizeof(block_t) * olen, GFP_KERNEL); 1067 if (!src_blkaddr) 1068 return -ENOMEM; 1069 1070 do_replace = kvzalloc(sizeof(int) * olen, GFP_KERNEL); 1071 if (!do_replace) { 1072 kvfree(src_blkaddr); 1073 return -ENOMEM; 1074 } 1075 1076 ret = __read_out_blkaddrs(src_inode, src_blkaddr, 1077 do_replace, src, olen); 1078 if (ret) 1079 goto roll_back; 1080 1081 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr, 1082 do_replace, src, dst, olen, full); 1083 if (ret) 1084 goto roll_back; 1085 1086 src += olen; 1087 dst += olen; 1088 len -= olen; 1089 1090 kvfree(src_blkaddr); 1091 kvfree(do_replace); 1092 } 1093 return 0; 1094 1095 roll_back: 1096 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len); 1097 kvfree(src_blkaddr); 1098 kvfree(do_replace); 1099 return ret; 1100 } 1101 1102 static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end) 1103 { 1104 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1105 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE; 1106 int ret; 1107 1108 f2fs_balance_fs(sbi, true); 1109 f2fs_lock_op(sbi); 1110 1111 f2fs_drop_extent_tree(inode); 1112 1113 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true); 1114 f2fs_unlock_op(sbi); 1115 return ret; 1116 } 1117 1118 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len) 1119 { 1120 pgoff_t pg_start, pg_end; 1121 loff_t new_size; 1122 int ret; 1123 1124 if (offset + len >= i_size_read(inode)) 1125 return -EINVAL; 1126 1127 /* collapse range should be aligned to block size of f2fs. */ 1128 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) 1129 return -EINVAL; 1130 1131 ret = f2fs_convert_inline_inode(inode); 1132 if (ret) 1133 return ret; 1134 1135 pg_start = offset >> PAGE_SHIFT; 1136 pg_end = (offset + len) >> PAGE_SHIFT; 1137 1138 down_write(&F2FS_I(inode)->i_mmap_sem); 1139 /* write out all dirty pages from offset */ 1140 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 1141 if (ret) 1142 goto out; 1143 1144 truncate_pagecache(inode, offset); 1145 1146 ret = f2fs_do_collapse(inode, pg_start, pg_end); 1147 if (ret) 1148 goto out; 1149 1150 /* write out all moved pages, if possible */ 1151 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 1152 truncate_pagecache(inode, offset); 1153 1154 new_size = i_size_read(inode) - len; 1155 truncate_pagecache(inode, new_size); 1156 1157 ret = truncate_blocks(inode, new_size, true); 1158 if (!ret) 1159 f2fs_i_size_write(inode, new_size); 1160 1161 out: 1162 up_write(&F2FS_I(inode)->i_mmap_sem); 1163 return ret; 1164 } 1165 1166 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start, 1167 pgoff_t end) 1168 { 1169 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); 1170 pgoff_t index = start; 1171 unsigned int ofs_in_node = dn->ofs_in_node; 1172 blkcnt_t count = 0; 1173 int ret; 1174 1175 for (; index < end; index++, dn->ofs_in_node++) { 1176 if (datablock_addr(dn->node_page, dn->ofs_in_node) == NULL_ADDR) 1177 count++; 1178 } 1179 1180 dn->ofs_in_node = ofs_in_node; 1181 ret = reserve_new_blocks(dn, count); 1182 if (ret) 1183 return ret; 1184 1185 dn->ofs_in_node = ofs_in_node; 1186 for (index = start; index < end; index++, dn->ofs_in_node++) { 1187 dn->data_blkaddr = 1188 datablock_addr(dn->node_page, dn->ofs_in_node); 1189 /* 1190 * reserve_new_blocks will not guarantee entire block 1191 * allocation. 1192 */ 1193 if (dn->data_blkaddr == NULL_ADDR) { 1194 ret = -ENOSPC; 1195 break; 1196 } 1197 if (dn->data_blkaddr != NEW_ADDR) { 1198 invalidate_blocks(sbi, dn->data_blkaddr); 1199 dn->data_blkaddr = NEW_ADDR; 1200 set_data_blkaddr(dn); 1201 } 1202 } 1203 1204 f2fs_update_extent_cache_range(dn, start, 0, index - start); 1205 1206 return ret; 1207 } 1208 1209 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len, 1210 int mode) 1211 { 1212 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1213 struct address_space *mapping = inode->i_mapping; 1214 pgoff_t index, pg_start, pg_end; 1215 loff_t new_size = i_size_read(inode); 1216 loff_t off_start, off_end; 1217 int ret = 0; 1218 1219 ret = inode_newsize_ok(inode, (len + offset)); 1220 if (ret) 1221 return ret; 1222 1223 ret = f2fs_convert_inline_inode(inode); 1224 if (ret) 1225 return ret; 1226 1227 down_write(&F2FS_I(inode)->i_mmap_sem); 1228 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1); 1229 if (ret) 1230 goto out_sem; 1231 1232 truncate_pagecache_range(inode, offset, offset + len - 1); 1233 1234 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT; 1235 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT; 1236 1237 off_start = offset & (PAGE_SIZE - 1); 1238 off_end = (offset + len) & (PAGE_SIZE - 1); 1239 1240 if (pg_start == pg_end) { 1241 ret = fill_zero(inode, pg_start, off_start, 1242 off_end - off_start); 1243 if (ret) 1244 goto out_sem; 1245 1246 new_size = max_t(loff_t, new_size, offset + len); 1247 } else { 1248 if (off_start) { 1249 ret = fill_zero(inode, pg_start++, off_start, 1250 PAGE_SIZE - off_start); 1251 if (ret) 1252 goto out_sem; 1253 1254 new_size = max_t(loff_t, new_size, 1255 (loff_t)pg_start << PAGE_SHIFT); 1256 } 1257 1258 for (index = pg_start; index < pg_end;) { 1259 struct dnode_of_data dn; 1260 unsigned int end_offset; 1261 pgoff_t end; 1262 1263 f2fs_lock_op(sbi); 1264 1265 set_new_dnode(&dn, inode, NULL, NULL, 0); 1266 ret = get_dnode_of_data(&dn, index, ALLOC_NODE); 1267 if (ret) { 1268 f2fs_unlock_op(sbi); 1269 goto out; 1270 } 1271 1272 end_offset = ADDRS_PER_PAGE(dn.node_page, inode); 1273 end = min(pg_end, end_offset - dn.ofs_in_node + index); 1274 1275 ret = f2fs_do_zero_range(&dn, index, end); 1276 f2fs_put_dnode(&dn); 1277 f2fs_unlock_op(sbi); 1278 1279 f2fs_balance_fs(sbi, dn.node_changed); 1280 1281 if (ret) 1282 goto out; 1283 1284 index = end; 1285 new_size = max_t(loff_t, new_size, 1286 (loff_t)index << PAGE_SHIFT); 1287 } 1288 1289 if (off_end) { 1290 ret = fill_zero(inode, pg_end, 0, off_end); 1291 if (ret) 1292 goto out; 1293 1294 new_size = max_t(loff_t, new_size, offset + len); 1295 } 1296 } 1297 1298 out: 1299 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size) 1300 f2fs_i_size_write(inode, new_size); 1301 out_sem: 1302 up_write(&F2FS_I(inode)->i_mmap_sem); 1303 1304 return ret; 1305 } 1306 1307 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len) 1308 { 1309 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1310 pgoff_t nr, pg_start, pg_end, delta, idx; 1311 loff_t new_size; 1312 int ret = 0; 1313 1314 new_size = i_size_read(inode) + len; 1315 ret = inode_newsize_ok(inode, new_size); 1316 if (ret) 1317 return ret; 1318 1319 if (offset >= i_size_read(inode)) 1320 return -EINVAL; 1321 1322 /* insert range should be aligned to block size of f2fs. */ 1323 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) 1324 return -EINVAL; 1325 1326 ret = f2fs_convert_inline_inode(inode); 1327 if (ret) 1328 return ret; 1329 1330 f2fs_balance_fs(sbi, true); 1331 1332 down_write(&F2FS_I(inode)->i_mmap_sem); 1333 ret = truncate_blocks(inode, i_size_read(inode), true); 1334 if (ret) 1335 goto out; 1336 1337 /* write out all dirty pages from offset */ 1338 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 1339 if (ret) 1340 goto out; 1341 1342 truncate_pagecache(inode, offset); 1343 1344 pg_start = offset >> PAGE_SHIFT; 1345 pg_end = (offset + len) >> PAGE_SHIFT; 1346 delta = pg_end - pg_start; 1347 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE; 1348 1349 while (!ret && idx > pg_start) { 1350 nr = idx - pg_start; 1351 if (nr > delta) 1352 nr = delta; 1353 idx -= nr; 1354 1355 f2fs_lock_op(sbi); 1356 f2fs_drop_extent_tree(inode); 1357 1358 ret = __exchange_data_block(inode, inode, idx, 1359 idx + delta, nr, false); 1360 f2fs_unlock_op(sbi); 1361 } 1362 1363 /* write out all moved pages, if possible */ 1364 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); 1365 truncate_pagecache(inode, offset); 1366 1367 if (!ret) 1368 f2fs_i_size_write(inode, new_size); 1369 out: 1370 up_write(&F2FS_I(inode)->i_mmap_sem); 1371 return ret; 1372 } 1373 1374 static int expand_inode_data(struct inode *inode, loff_t offset, 1375 loff_t len, int mode) 1376 { 1377 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1378 struct f2fs_map_blocks map = { .m_next_pgofs = NULL }; 1379 pgoff_t pg_end; 1380 loff_t new_size = i_size_read(inode); 1381 loff_t off_end; 1382 int err; 1383 1384 err = inode_newsize_ok(inode, (len + offset)); 1385 if (err) 1386 return err; 1387 1388 err = f2fs_convert_inline_inode(inode); 1389 if (err) 1390 return err; 1391 1392 f2fs_balance_fs(sbi, true); 1393 1394 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT; 1395 off_end = (offset + len) & (PAGE_SIZE - 1); 1396 1397 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT; 1398 map.m_len = pg_end - map.m_lblk; 1399 if (off_end) 1400 map.m_len++; 1401 1402 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO); 1403 if (err) { 1404 pgoff_t last_off; 1405 1406 if (!map.m_len) 1407 return err; 1408 1409 last_off = map.m_lblk + map.m_len - 1; 1410 1411 /* update new size to the failed position */ 1412 new_size = (last_off == pg_end) ? offset + len: 1413 (loff_t)(last_off + 1) << PAGE_SHIFT; 1414 } else { 1415 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end; 1416 } 1417 1418 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size) 1419 f2fs_i_size_write(inode, new_size); 1420 1421 return err; 1422 } 1423 1424 static long f2fs_fallocate(struct file *file, int mode, 1425 loff_t offset, loff_t len) 1426 { 1427 struct inode *inode = file_inode(file); 1428 long ret = 0; 1429 1430 /* f2fs only support ->fallocate for regular file */ 1431 if (!S_ISREG(inode->i_mode)) 1432 return -EINVAL; 1433 1434 if (f2fs_encrypted_inode(inode) && 1435 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) 1436 return -EOPNOTSUPP; 1437 1438 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | 1439 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | 1440 FALLOC_FL_INSERT_RANGE)) 1441 return -EOPNOTSUPP; 1442 1443 inode_lock(inode); 1444 1445 if (mode & FALLOC_FL_PUNCH_HOLE) { 1446 if (offset >= inode->i_size) 1447 goto out; 1448 1449 ret = punch_hole(inode, offset, len); 1450 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { 1451 ret = f2fs_collapse_range(inode, offset, len); 1452 } else if (mode & FALLOC_FL_ZERO_RANGE) { 1453 ret = f2fs_zero_range(inode, offset, len, mode); 1454 } else if (mode & FALLOC_FL_INSERT_RANGE) { 1455 ret = f2fs_insert_range(inode, offset, len); 1456 } else { 1457 ret = expand_inode_data(inode, offset, len, mode); 1458 } 1459 1460 if (!ret) { 1461 inode->i_mtime = inode->i_ctime = current_time(inode); 1462 f2fs_mark_inode_dirty_sync(inode, false); 1463 if (mode & FALLOC_FL_KEEP_SIZE) 1464 file_set_keep_isize(inode); 1465 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1466 } 1467 1468 out: 1469 inode_unlock(inode); 1470 1471 trace_f2fs_fallocate(inode, mode, offset, len, ret); 1472 return ret; 1473 } 1474 1475 static int f2fs_release_file(struct inode *inode, struct file *filp) 1476 { 1477 /* 1478 * f2fs_relase_file is called at every close calls. So we should 1479 * not drop any inmemory pages by close called by other process. 1480 */ 1481 if (!(filp->f_mode & FMODE_WRITE) || 1482 atomic_read(&inode->i_writecount) != 1) 1483 return 0; 1484 1485 /* some remained atomic pages should discarded */ 1486 if (f2fs_is_atomic_file(inode)) 1487 drop_inmem_pages(inode); 1488 if (f2fs_is_volatile_file(inode)) { 1489 clear_inode_flag(inode, FI_VOLATILE_FILE); 1490 stat_dec_volatile_write(inode); 1491 set_inode_flag(inode, FI_DROP_CACHE); 1492 filemap_fdatawrite(inode->i_mapping); 1493 clear_inode_flag(inode, FI_DROP_CACHE); 1494 } 1495 return 0; 1496 } 1497 1498 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL)) 1499 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL) 1500 1501 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags) 1502 { 1503 if (S_ISDIR(mode)) 1504 return flags; 1505 else if (S_ISREG(mode)) 1506 return flags & F2FS_REG_FLMASK; 1507 else 1508 return flags & F2FS_OTHER_FLMASK; 1509 } 1510 1511 static int f2fs_ioc_getflags(struct file *filp, unsigned long arg) 1512 { 1513 struct inode *inode = file_inode(filp); 1514 struct f2fs_inode_info *fi = F2FS_I(inode); 1515 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE; 1516 return put_user(flags, (int __user *)arg); 1517 } 1518 1519 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg) 1520 { 1521 struct inode *inode = file_inode(filp); 1522 struct f2fs_inode_info *fi = F2FS_I(inode); 1523 unsigned int flags; 1524 unsigned int oldflags; 1525 int ret; 1526 1527 if (!inode_owner_or_capable(inode)) 1528 return -EACCES; 1529 1530 if (get_user(flags, (int __user *)arg)) 1531 return -EFAULT; 1532 1533 ret = mnt_want_write_file(filp); 1534 if (ret) 1535 return ret; 1536 1537 inode_lock(inode); 1538 1539 /* Is it quota file? Do not allow user to mess with it */ 1540 if (IS_NOQUOTA(inode)) { 1541 inode_unlock(inode); 1542 ret = -EPERM; 1543 goto unlock_out; 1544 } 1545 1546 flags = f2fs_mask_flags(inode->i_mode, flags); 1547 1548 oldflags = fi->i_flags; 1549 1550 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 1551 if (!capable(CAP_LINUX_IMMUTABLE)) { 1552 inode_unlock(inode); 1553 ret = -EPERM; 1554 goto out; 1555 } 1556 } 1557 1558 flags = flags & FS_FL_USER_MODIFIABLE; 1559 flags |= oldflags & ~FS_FL_USER_MODIFIABLE; 1560 fi->i_flags = flags; 1561 1562 inode->i_ctime = current_time(inode); 1563 f2fs_set_inode_flags(inode); 1564 f2fs_mark_inode_dirty_sync(inode, false); 1565 unlock_out: 1566 inode_unlock(inode); 1567 out: 1568 mnt_drop_write_file(filp); 1569 return ret; 1570 } 1571 1572 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg) 1573 { 1574 struct inode *inode = file_inode(filp); 1575 1576 return put_user(inode->i_generation, (int __user *)arg); 1577 } 1578 1579 static int f2fs_ioc_start_atomic_write(struct file *filp) 1580 { 1581 struct inode *inode = file_inode(filp); 1582 int ret; 1583 1584 if (!inode_owner_or_capable(inode)) 1585 return -EACCES; 1586 1587 if (!S_ISREG(inode->i_mode)) 1588 return -EINVAL; 1589 1590 ret = mnt_want_write_file(filp); 1591 if (ret) 1592 return ret; 1593 1594 inode_lock(inode); 1595 1596 if (f2fs_is_atomic_file(inode)) 1597 goto out; 1598 1599 ret = f2fs_convert_inline_inode(inode); 1600 if (ret) 1601 goto out; 1602 1603 set_inode_flag(inode, FI_ATOMIC_FILE); 1604 set_inode_flag(inode, FI_HOT_DATA); 1605 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1606 1607 if (!get_dirty_pages(inode)) 1608 goto inc_stat; 1609 1610 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING, 1611 "Unexpected flush for atomic writes: ino=%lu, npages=%u", 1612 inode->i_ino, get_dirty_pages(inode)); 1613 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX); 1614 if (ret) { 1615 clear_inode_flag(inode, FI_ATOMIC_FILE); 1616 goto out; 1617 } 1618 1619 inc_stat: 1620 stat_inc_atomic_write(inode); 1621 stat_update_max_atomic_write(inode); 1622 out: 1623 inode_unlock(inode); 1624 mnt_drop_write_file(filp); 1625 return ret; 1626 } 1627 1628 static int f2fs_ioc_commit_atomic_write(struct file *filp) 1629 { 1630 struct inode *inode = file_inode(filp); 1631 int ret; 1632 1633 if (!inode_owner_or_capable(inode)) 1634 return -EACCES; 1635 1636 ret = mnt_want_write_file(filp); 1637 if (ret) 1638 return ret; 1639 1640 inode_lock(inode); 1641 1642 if (f2fs_is_volatile_file(inode)) 1643 goto err_out; 1644 1645 if (f2fs_is_atomic_file(inode)) { 1646 ret = commit_inmem_pages(inode); 1647 if (ret) 1648 goto err_out; 1649 1650 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true); 1651 if (!ret) { 1652 clear_inode_flag(inode, FI_ATOMIC_FILE); 1653 stat_dec_atomic_write(inode); 1654 } 1655 } else { 1656 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true); 1657 } 1658 err_out: 1659 inode_unlock(inode); 1660 mnt_drop_write_file(filp); 1661 return ret; 1662 } 1663 1664 static int f2fs_ioc_start_volatile_write(struct file *filp) 1665 { 1666 struct inode *inode = file_inode(filp); 1667 int ret; 1668 1669 if (!inode_owner_or_capable(inode)) 1670 return -EACCES; 1671 1672 if (!S_ISREG(inode->i_mode)) 1673 return -EINVAL; 1674 1675 ret = mnt_want_write_file(filp); 1676 if (ret) 1677 return ret; 1678 1679 inode_lock(inode); 1680 1681 if (f2fs_is_volatile_file(inode)) 1682 goto out; 1683 1684 ret = f2fs_convert_inline_inode(inode); 1685 if (ret) 1686 goto out; 1687 1688 stat_inc_volatile_write(inode); 1689 stat_update_max_volatile_write(inode); 1690 1691 set_inode_flag(inode, FI_VOLATILE_FILE); 1692 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1693 out: 1694 inode_unlock(inode); 1695 mnt_drop_write_file(filp); 1696 return ret; 1697 } 1698 1699 static int f2fs_ioc_release_volatile_write(struct file *filp) 1700 { 1701 struct inode *inode = file_inode(filp); 1702 int ret; 1703 1704 if (!inode_owner_or_capable(inode)) 1705 return -EACCES; 1706 1707 ret = mnt_want_write_file(filp); 1708 if (ret) 1709 return ret; 1710 1711 inode_lock(inode); 1712 1713 if (!f2fs_is_volatile_file(inode)) 1714 goto out; 1715 1716 if (!f2fs_is_first_block_written(inode)) { 1717 ret = truncate_partial_data_page(inode, 0, true); 1718 goto out; 1719 } 1720 1721 ret = punch_hole(inode, 0, F2FS_BLKSIZE); 1722 out: 1723 inode_unlock(inode); 1724 mnt_drop_write_file(filp); 1725 return ret; 1726 } 1727 1728 static int f2fs_ioc_abort_volatile_write(struct file *filp) 1729 { 1730 struct inode *inode = file_inode(filp); 1731 int ret; 1732 1733 if (!inode_owner_or_capable(inode)) 1734 return -EACCES; 1735 1736 ret = mnt_want_write_file(filp); 1737 if (ret) 1738 return ret; 1739 1740 inode_lock(inode); 1741 1742 if (f2fs_is_atomic_file(inode)) 1743 drop_inmem_pages(inode); 1744 if (f2fs_is_volatile_file(inode)) { 1745 clear_inode_flag(inode, FI_VOLATILE_FILE); 1746 stat_dec_volatile_write(inode); 1747 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true); 1748 } 1749 1750 inode_unlock(inode); 1751 1752 mnt_drop_write_file(filp); 1753 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1754 return ret; 1755 } 1756 1757 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg) 1758 { 1759 struct inode *inode = file_inode(filp); 1760 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1761 struct super_block *sb = sbi->sb; 1762 __u32 in; 1763 int ret; 1764 1765 if (!capable(CAP_SYS_ADMIN)) 1766 return -EPERM; 1767 1768 if (get_user(in, (__u32 __user *)arg)) 1769 return -EFAULT; 1770 1771 ret = mnt_want_write_file(filp); 1772 if (ret) 1773 return ret; 1774 1775 switch (in) { 1776 case F2FS_GOING_DOWN_FULLSYNC: 1777 sb = freeze_bdev(sb->s_bdev); 1778 if (sb && !IS_ERR(sb)) { 1779 f2fs_stop_checkpoint(sbi, false); 1780 thaw_bdev(sb->s_bdev, sb); 1781 } 1782 break; 1783 case F2FS_GOING_DOWN_METASYNC: 1784 /* do checkpoint only */ 1785 f2fs_sync_fs(sb, 1); 1786 f2fs_stop_checkpoint(sbi, false); 1787 break; 1788 case F2FS_GOING_DOWN_NOSYNC: 1789 f2fs_stop_checkpoint(sbi, false); 1790 break; 1791 case F2FS_GOING_DOWN_METAFLUSH: 1792 sync_meta_pages(sbi, META, LONG_MAX); 1793 f2fs_stop_checkpoint(sbi, false); 1794 break; 1795 default: 1796 ret = -EINVAL; 1797 goto out; 1798 } 1799 f2fs_update_time(sbi, REQ_TIME); 1800 out: 1801 mnt_drop_write_file(filp); 1802 return ret; 1803 } 1804 1805 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg) 1806 { 1807 struct inode *inode = file_inode(filp); 1808 struct super_block *sb = inode->i_sb; 1809 struct request_queue *q = bdev_get_queue(sb->s_bdev); 1810 struct fstrim_range range; 1811 int ret; 1812 1813 if (!capable(CAP_SYS_ADMIN)) 1814 return -EPERM; 1815 1816 if (!blk_queue_discard(q)) 1817 return -EOPNOTSUPP; 1818 1819 if (copy_from_user(&range, (struct fstrim_range __user *)arg, 1820 sizeof(range))) 1821 return -EFAULT; 1822 1823 ret = mnt_want_write_file(filp); 1824 if (ret) 1825 return ret; 1826 1827 range.minlen = max((unsigned int)range.minlen, 1828 q->limits.discard_granularity); 1829 ret = f2fs_trim_fs(F2FS_SB(sb), &range); 1830 mnt_drop_write_file(filp); 1831 if (ret < 0) 1832 return ret; 1833 1834 if (copy_to_user((struct fstrim_range __user *)arg, &range, 1835 sizeof(range))) 1836 return -EFAULT; 1837 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1838 return 0; 1839 } 1840 1841 static bool uuid_is_nonzero(__u8 u[16]) 1842 { 1843 int i; 1844 1845 for (i = 0; i < 16; i++) 1846 if (u[i]) 1847 return true; 1848 return false; 1849 } 1850 1851 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg) 1852 { 1853 struct inode *inode = file_inode(filp); 1854 1855 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); 1856 1857 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); 1858 } 1859 1860 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg) 1861 { 1862 return fscrypt_ioctl_get_policy(filp, (void __user *)arg); 1863 } 1864 1865 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg) 1866 { 1867 struct inode *inode = file_inode(filp); 1868 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1869 int err; 1870 1871 if (!f2fs_sb_has_crypto(inode->i_sb)) 1872 return -EOPNOTSUPP; 1873 1874 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt)) 1875 goto got_it; 1876 1877 err = mnt_want_write_file(filp); 1878 if (err) 1879 return err; 1880 1881 /* update superblock with uuid */ 1882 generate_random_uuid(sbi->raw_super->encrypt_pw_salt); 1883 1884 err = f2fs_commit_super(sbi, false); 1885 if (err) { 1886 /* undo new data */ 1887 memset(sbi->raw_super->encrypt_pw_salt, 0, 16); 1888 mnt_drop_write_file(filp); 1889 return err; 1890 } 1891 mnt_drop_write_file(filp); 1892 got_it: 1893 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt, 1894 16)) 1895 return -EFAULT; 1896 return 0; 1897 } 1898 1899 static int f2fs_ioc_gc(struct file *filp, unsigned long arg) 1900 { 1901 struct inode *inode = file_inode(filp); 1902 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1903 __u32 sync; 1904 int ret; 1905 1906 if (!capable(CAP_SYS_ADMIN)) 1907 return -EPERM; 1908 1909 if (get_user(sync, (__u32 __user *)arg)) 1910 return -EFAULT; 1911 1912 if (f2fs_readonly(sbi->sb)) 1913 return -EROFS; 1914 1915 ret = mnt_want_write_file(filp); 1916 if (ret) 1917 return ret; 1918 1919 if (!sync) { 1920 if (!mutex_trylock(&sbi->gc_mutex)) { 1921 ret = -EBUSY; 1922 goto out; 1923 } 1924 } else { 1925 mutex_lock(&sbi->gc_mutex); 1926 } 1927 1928 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO); 1929 out: 1930 mnt_drop_write_file(filp); 1931 return ret; 1932 } 1933 1934 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg) 1935 { 1936 struct inode *inode = file_inode(filp); 1937 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1938 struct f2fs_gc_range range; 1939 u64 end; 1940 int ret; 1941 1942 if (!capable(CAP_SYS_ADMIN)) 1943 return -EPERM; 1944 1945 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg, 1946 sizeof(range))) 1947 return -EFAULT; 1948 1949 if (f2fs_readonly(sbi->sb)) 1950 return -EROFS; 1951 1952 ret = mnt_want_write_file(filp); 1953 if (ret) 1954 return ret; 1955 1956 end = range.start + range.len; 1957 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi)) 1958 return -EINVAL; 1959 do_more: 1960 if (!range.sync) { 1961 if (!mutex_trylock(&sbi->gc_mutex)) { 1962 ret = -EBUSY; 1963 goto out; 1964 } 1965 } else { 1966 mutex_lock(&sbi->gc_mutex); 1967 } 1968 1969 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start)); 1970 range.start += sbi->blocks_per_seg; 1971 if (range.start <= end) 1972 goto do_more; 1973 out: 1974 mnt_drop_write_file(filp); 1975 return ret; 1976 } 1977 1978 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg) 1979 { 1980 struct inode *inode = file_inode(filp); 1981 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1982 int ret; 1983 1984 if (!capable(CAP_SYS_ADMIN)) 1985 return -EPERM; 1986 1987 if (f2fs_readonly(sbi->sb)) 1988 return -EROFS; 1989 1990 ret = mnt_want_write_file(filp); 1991 if (ret) 1992 return ret; 1993 1994 ret = f2fs_sync_fs(sbi->sb, 1); 1995 1996 mnt_drop_write_file(filp); 1997 return ret; 1998 } 1999 2000 static int f2fs_defragment_range(struct f2fs_sb_info *sbi, 2001 struct file *filp, 2002 struct f2fs_defragment *range) 2003 { 2004 struct inode *inode = file_inode(filp); 2005 struct f2fs_map_blocks map = { .m_next_pgofs = NULL }; 2006 struct extent_info ei = {0,0,0}; 2007 pgoff_t pg_start, pg_end; 2008 unsigned int blk_per_seg = sbi->blocks_per_seg; 2009 unsigned int total = 0, sec_num; 2010 block_t blk_end = 0; 2011 bool fragmented = false; 2012 int err; 2013 2014 /* if in-place-update policy is enabled, don't waste time here */ 2015 if (need_inplace_update_policy(inode, NULL)) 2016 return -EINVAL; 2017 2018 pg_start = range->start >> PAGE_SHIFT; 2019 pg_end = (range->start + range->len) >> PAGE_SHIFT; 2020 2021 f2fs_balance_fs(sbi, true); 2022 2023 inode_lock(inode); 2024 2025 /* writeback all dirty pages in the range */ 2026 err = filemap_write_and_wait_range(inode->i_mapping, range->start, 2027 range->start + range->len - 1); 2028 if (err) 2029 goto out; 2030 2031 /* 2032 * lookup mapping info in extent cache, skip defragmenting if physical 2033 * block addresses are continuous. 2034 */ 2035 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) { 2036 if (ei.fofs + ei.len >= pg_end) 2037 goto out; 2038 } 2039 2040 map.m_lblk = pg_start; 2041 2042 /* 2043 * lookup mapping info in dnode page cache, skip defragmenting if all 2044 * physical block addresses are continuous even if there are hole(s) 2045 * in logical blocks. 2046 */ 2047 while (map.m_lblk < pg_end) { 2048 map.m_len = pg_end - map.m_lblk; 2049 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ); 2050 if (err) 2051 goto out; 2052 2053 if (!(map.m_flags & F2FS_MAP_FLAGS)) { 2054 map.m_lblk++; 2055 continue; 2056 } 2057 2058 if (blk_end && blk_end != map.m_pblk) { 2059 fragmented = true; 2060 break; 2061 } 2062 blk_end = map.m_pblk + map.m_len; 2063 2064 map.m_lblk += map.m_len; 2065 } 2066 2067 if (!fragmented) 2068 goto out; 2069 2070 map.m_lblk = pg_start; 2071 map.m_len = pg_end - pg_start; 2072 2073 sec_num = (map.m_len + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi); 2074 2075 /* 2076 * make sure there are enough free section for LFS allocation, this can 2077 * avoid defragment running in SSR mode when free section are allocated 2078 * intensively 2079 */ 2080 if (has_not_enough_free_secs(sbi, 0, sec_num)) { 2081 err = -EAGAIN; 2082 goto out; 2083 } 2084 2085 while (map.m_lblk < pg_end) { 2086 pgoff_t idx; 2087 int cnt = 0; 2088 2089 do_map: 2090 map.m_len = pg_end - map.m_lblk; 2091 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ); 2092 if (err) 2093 goto clear_out; 2094 2095 if (!(map.m_flags & F2FS_MAP_FLAGS)) { 2096 map.m_lblk++; 2097 continue; 2098 } 2099 2100 set_inode_flag(inode, FI_DO_DEFRAG); 2101 2102 idx = map.m_lblk; 2103 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) { 2104 struct page *page; 2105 2106 page = get_lock_data_page(inode, idx, true); 2107 if (IS_ERR(page)) { 2108 err = PTR_ERR(page); 2109 goto clear_out; 2110 } 2111 2112 set_page_dirty(page); 2113 f2fs_put_page(page, 1); 2114 2115 idx++; 2116 cnt++; 2117 total++; 2118 } 2119 2120 map.m_lblk = idx; 2121 2122 if (idx < pg_end && cnt < blk_per_seg) 2123 goto do_map; 2124 2125 clear_inode_flag(inode, FI_DO_DEFRAG); 2126 2127 err = filemap_fdatawrite(inode->i_mapping); 2128 if (err) 2129 goto out; 2130 } 2131 clear_out: 2132 clear_inode_flag(inode, FI_DO_DEFRAG); 2133 out: 2134 inode_unlock(inode); 2135 if (!err) 2136 range->len = (u64)total << PAGE_SHIFT; 2137 return err; 2138 } 2139 2140 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg) 2141 { 2142 struct inode *inode = file_inode(filp); 2143 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2144 struct f2fs_defragment range; 2145 int err; 2146 2147 if (!capable(CAP_SYS_ADMIN)) 2148 return -EPERM; 2149 2150 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode)) 2151 return -EINVAL; 2152 2153 if (f2fs_readonly(sbi->sb)) 2154 return -EROFS; 2155 2156 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg, 2157 sizeof(range))) 2158 return -EFAULT; 2159 2160 /* verify alignment of offset & size */ 2161 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1)) 2162 return -EINVAL; 2163 2164 if (unlikely((range.start + range.len) >> PAGE_SHIFT > 2165 sbi->max_file_blocks)) 2166 return -EINVAL; 2167 2168 err = mnt_want_write_file(filp); 2169 if (err) 2170 return err; 2171 2172 err = f2fs_defragment_range(sbi, filp, &range); 2173 mnt_drop_write_file(filp); 2174 2175 f2fs_update_time(sbi, REQ_TIME); 2176 if (err < 0) 2177 return err; 2178 2179 if (copy_to_user((struct f2fs_defragment __user *)arg, &range, 2180 sizeof(range))) 2181 return -EFAULT; 2182 2183 return 0; 2184 } 2185 2186 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in, 2187 struct file *file_out, loff_t pos_out, size_t len) 2188 { 2189 struct inode *src = file_inode(file_in); 2190 struct inode *dst = file_inode(file_out); 2191 struct f2fs_sb_info *sbi = F2FS_I_SB(src); 2192 size_t olen = len, dst_max_i_size = 0; 2193 size_t dst_osize; 2194 int ret; 2195 2196 if (file_in->f_path.mnt != file_out->f_path.mnt || 2197 src->i_sb != dst->i_sb) 2198 return -EXDEV; 2199 2200 if (unlikely(f2fs_readonly(src->i_sb))) 2201 return -EROFS; 2202 2203 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode)) 2204 return -EINVAL; 2205 2206 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst)) 2207 return -EOPNOTSUPP; 2208 2209 if (src == dst) { 2210 if (pos_in == pos_out) 2211 return 0; 2212 if (pos_out > pos_in && pos_out < pos_in + len) 2213 return -EINVAL; 2214 } 2215 2216 inode_lock(src); 2217 if (src != dst) { 2218 if (!inode_trylock(dst)) { 2219 ret = -EBUSY; 2220 goto out; 2221 } 2222 } 2223 2224 ret = -EINVAL; 2225 if (pos_in + len > src->i_size || pos_in + len < pos_in) 2226 goto out_unlock; 2227 if (len == 0) 2228 olen = len = src->i_size - pos_in; 2229 if (pos_in + len == src->i_size) 2230 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in; 2231 if (len == 0) { 2232 ret = 0; 2233 goto out_unlock; 2234 } 2235 2236 dst_osize = dst->i_size; 2237 if (pos_out + olen > dst->i_size) 2238 dst_max_i_size = pos_out + olen; 2239 2240 /* verify the end result is block aligned */ 2241 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) || 2242 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) || 2243 !IS_ALIGNED(pos_out, F2FS_BLKSIZE)) 2244 goto out_unlock; 2245 2246 ret = f2fs_convert_inline_inode(src); 2247 if (ret) 2248 goto out_unlock; 2249 2250 ret = f2fs_convert_inline_inode(dst); 2251 if (ret) 2252 goto out_unlock; 2253 2254 /* write out all dirty pages from offset */ 2255 ret = filemap_write_and_wait_range(src->i_mapping, 2256 pos_in, pos_in + len); 2257 if (ret) 2258 goto out_unlock; 2259 2260 ret = filemap_write_and_wait_range(dst->i_mapping, 2261 pos_out, pos_out + len); 2262 if (ret) 2263 goto out_unlock; 2264 2265 f2fs_balance_fs(sbi, true); 2266 f2fs_lock_op(sbi); 2267 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS, 2268 pos_out >> F2FS_BLKSIZE_BITS, 2269 len >> F2FS_BLKSIZE_BITS, false); 2270 2271 if (!ret) { 2272 if (dst_max_i_size) 2273 f2fs_i_size_write(dst, dst_max_i_size); 2274 else if (dst_osize != dst->i_size) 2275 f2fs_i_size_write(dst, dst_osize); 2276 } 2277 f2fs_unlock_op(sbi); 2278 out_unlock: 2279 if (src != dst) 2280 inode_unlock(dst); 2281 out: 2282 inode_unlock(src); 2283 return ret; 2284 } 2285 2286 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg) 2287 { 2288 struct f2fs_move_range range; 2289 struct fd dst; 2290 int err; 2291 2292 if (!(filp->f_mode & FMODE_READ) || 2293 !(filp->f_mode & FMODE_WRITE)) 2294 return -EBADF; 2295 2296 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg, 2297 sizeof(range))) 2298 return -EFAULT; 2299 2300 dst = fdget(range.dst_fd); 2301 if (!dst.file) 2302 return -EBADF; 2303 2304 if (!(dst.file->f_mode & FMODE_WRITE)) { 2305 err = -EBADF; 2306 goto err_out; 2307 } 2308 2309 err = mnt_want_write_file(filp); 2310 if (err) 2311 goto err_out; 2312 2313 err = f2fs_move_file_range(filp, range.pos_in, dst.file, 2314 range.pos_out, range.len); 2315 2316 mnt_drop_write_file(filp); 2317 if (err) 2318 goto err_out; 2319 2320 if (copy_to_user((struct f2fs_move_range __user *)arg, 2321 &range, sizeof(range))) 2322 err = -EFAULT; 2323 err_out: 2324 fdput(dst); 2325 return err; 2326 } 2327 2328 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg) 2329 { 2330 struct inode *inode = file_inode(filp); 2331 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2332 struct sit_info *sm = SIT_I(sbi); 2333 unsigned int start_segno = 0, end_segno = 0; 2334 unsigned int dev_start_segno = 0, dev_end_segno = 0; 2335 struct f2fs_flush_device range; 2336 int ret; 2337 2338 if (!capable(CAP_SYS_ADMIN)) 2339 return -EPERM; 2340 2341 if (f2fs_readonly(sbi->sb)) 2342 return -EROFS; 2343 2344 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg, 2345 sizeof(range))) 2346 return -EFAULT; 2347 2348 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num || 2349 sbi->segs_per_sec != 1) { 2350 f2fs_msg(sbi->sb, KERN_WARNING, 2351 "Can't flush %u in %d for segs_per_sec %u != 1\n", 2352 range.dev_num, sbi->s_ndevs, 2353 sbi->segs_per_sec); 2354 return -EINVAL; 2355 } 2356 2357 ret = mnt_want_write_file(filp); 2358 if (ret) 2359 return ret; 2360 2361 if (range.dev_num != 0) 2362 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk); 2363 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk); 2364 2365 start_segno = sm->last_victim[FLUSH_DEVICE]; 2366 if (start_segno < dev_start_segno || start_segno >= dev_end_segno) 2367 start_segno = dev_start_segno; 2368 end_segno = min(start_segno + range.segments, dev_end_segno); 2369 2370 while (start_segno < end_segno) { 2371 if (!mutex_trylock(&sbi->gc_mutex)) { 2372 ret = -EBUSY; 2373 goto out; 2374 } 2375 sm->last_victim[GC_CB] = end_segno + 1; 2376 sm->last_victim[GC_GREEDY] = end_segno + 1; 2377 sm->last_victim[ALLOC_NEXT] = end_segno + 1; 2378 ret = f2fs_gc(sbi, true, true, start_segno); 2379 if (ret == -EAGAIN) 2380 ret = 0; 2381 else if (ret < 0) 2382 break; 2383 start_segno++; 2384 } 2385 out: 2386 mnt_drop_write_file(filp); 2387 return ret; 2388 } 2389 2390 2391 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 2392 { 2393 switch (cmd) { 2394 case F2FS_IOC_GETFLAGS: 2395 return f2fs_ioc_getflags(filp, arg); 2396 case F2FS_IOC_SETFLAGS: 2397 return f2fs_ioc_setflags(filp, arg); 2398 case F2FS_IOC_GETVERSION: 2399 return f2fs_ioc_getversion(filp, arg); 2400 case F2FS_IOC_START_ATOMIC_WRITE: 2401 return f2fs_ioc_start_atomic_write(filp); 2402 case F2FS_IOC_COMMIT_ATOMIC_WRITE: 2403 return f2fs_ioc_commit_atomic_write(filp); 2404 case F2FS_IOC_START_VOLATILE_WRITE: 2405 return f2fs_ioc_start_volatile_write(filp); 2406 case F2FS_IOC_RELEASE_VOLATILE_WRITE: 2407 return f2fs_ioc_release_volatile_write(filp); 2408 case F2FS_IOC_ABORT_VOLATILE_WRITE: 2409 return f2fs_ioc_abort_volatile_write(filp); 2410 case F2FS_IOC_SHUTDOWN: 2411 return f2fs_ioc_shutdown(filp, arg); 2412 case FITRIM: 2413 return f2fs_ioc_fitrim(filp, arg); 2414 case F2FS_IOC_SET_ENCRYPTION_POLICY: 2415 return f2fs_ioc_set_encryption_policy(filp, arg); 2416 case F2FS_IOC_GET_ENCRYPTION_POLICY: 2417 return f2fs_ioc_get_encryption_policy(filp, arg); 2418 case F2FS_IOC_GET_ENCRYPTION_PWSALT: 2419 return f2fs_ioc_get_encryption_pwsalt(filp, arg); 2420 case F2FS_IOC_GARBAGE_COLLECT: 2421 return f2fs_ioc_gc(filp, arg); 2422 case F2FS_IOC_GARBAGE_COLLECT_RANGE: 2423 return f2fs_ioc_gc_range(filp, arg); 2424 case F2FS_IOC_WRITE_CHECKPOINT: 2425 return f2fs_ioc_write_checkpoint(filp, arg); 2426 case F2FS_IOC_DEFRAGMENT: 2427 return f2fs_ioc_defragment(filp, arg); 2428 case F2FS_IOC_MOVE_RANGE: 2429 return f2fs_ioc_move_range(filp, arg); 2430 case F2FS_IOC_FLUSH_DEVICE: 2431 return f2fs_ioc_flush_device(filp, arg); 2432 default: 2433 return -ENOTTY; 2434 } 2435 } 2436 2437 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 2438 { 2439 struct file *file = iocb->ki_filp; 2440 struct inode *inode = file_inode(file); 2441 struct blk_plug plug; 2442 ssize_t ret; 2443 2444 inode_lock(inode); 2445 ret = generic_write_checks(iocb, from); 2446 if (ret > 0) { 2447 int err; 2448 2449 if (iov_iter_fault_in_readable(from, iov_iter_count(from))) 2450 set_inode_flag(inode, FI_NO_PREALLOC); 2451 2452 err = f2fs_preallocate_blocks(iocb, from); 2453 if (err) { 2454 inode_unlock(inode); 2455 return err; 2456 } 2457 blk_start_plug(&plug); 2458 ret = __generic_file_write_iter(iocb, from); 2459 blk_finish_plug(&plug); 2460 clear_inode_flag(inode, FI_NO_PREALLOC); 2461 } 2462 inode_unlock(inode); 2463 2464 if (ret > 0) 2465 ret = generic_write_sync(iocb, ret); 2466 return ret; 2467 } 2468 2469 #ifdef CONFIG_COMPAT 2470 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2471 { 2472 switch (cmd) { 2473 case F2FS_IOC32_GETFLAGS: 2474 cmd = F2FS_IOC_GETFLAGS; 2475 break; 2476 case F2FS_IOC32_SETFLAGS: 2477 cmd = F2FS_IOC_SETFLAGS; 2478 break; 2479 case F2FS_IOC32_GETVERSION: 2480 cmd = F2FS_IOC_GETVERSION; 2481 break; 2482 case F2FS_IOC_START_ATOMIC_WRITE: 2483 case F2FS_IOC_COMMIT_ATOMIC_WRITE: 2484 case F2FS_IOC_START_VOLATILE_WRITE: 2485 case F2FS_IOC_RELEASE_VOLATILE_WRITE: 2486 case F2FS_IOC_ABORT_VOLATILE_WRITE: 2487 case F2FS_IOC_SHUTDOWN: 2488 case F2FS_IOC_SET_ENCRYPTION_POLICY: 2489 case F2FS_IOC_GET_ENCRYPTION_PWSALT: 2490 case F2FS_IOC_GET_ENCRYPTION_POLICY: 2491 case F2FS_IOC_GARBAGE_COLLECT: 2492 case F2FS_IOC_GARBAGE_COLLECT_RANGE: 2493 case F2FS_IOC_WRITE_CHECKPOINT: 2494 case F2FS_IOC_DEFRAGMENT: 2495 case F2FS_IOC_MOVE_RANGE: 2496 case F2FS_IOC_FLUSH_DEVICE: 2497 break; 2498 default: 2499 return -ENOIOCTLCMD; 2500 } 2501 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 2502 } 2503 #endif 2504 2505 const struct file_operations f2fs_file_operations = { 2506 .llseek = f2fs_llseek, 2507 .read_iter = generic_file_read_iter, 2508 .write_iter = f2fs_file_write_iter, 2509 .open = f2fs_file_open, 2510 .release = f2fs_release_file, 2511 .mmap = f2fs_file_mmap, 2512 .fsync = f2fs_sync_file, 2513 .fallocate = f2fs_fallocate, 2514 .unlocked_ioctl = f2fs_ioctl, 2515 #ifdef CONFIG_COMPAT 2516 .compat_ioctl = f2fs_compat_ioctl, 2517 #endif 2518 .splice_read = generic_file_splice_read, 2519 .splice_write = iter_file_splice_write, 2520 }; 2521