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 24 #include "f2fs.h" 25 #include "node.h" 26 #include "segment.h" 27 #include "xattr.h" 28 #include "acl.h" 29 #include <trace/events/f2fs.h> 30 31 static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma, 32 struct vm_fault *vmf) 33 { 34 struct page *page = vmf->page; 35 struct inode *inode = file_inode(vma->vm_file); 36 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 37 struct dnode_of_data dn; 38 int err; 39 40 f2fs_balance_fs(sbi); 41 42 sb_start_pagefault(inode->i_sb); 43 44 /* block allocation */ 45 f2fs_lock_op(sbi); 46 set_new_dnode(&dn, inode, NULL, NULL, 0); 47 err = f2fs_reserve_block(&dn, page->index); 48 f2fs_unlock_op(sbi); 49 if (err) 50 goto out; 51 52 file_update_time(vma->vm_file); 53 lock_page(page); 54 if (unlikely(page->mapping != inode->i_mapping || 55 page_offset(page) > i_size_read(inode) || 56 !PageUptodate(page))) { 57 unlock_page(page); 58 err = -EFAULT; 59 goto out; 60 } 61 62 /* 63 * check to see if the page is mapped already (no holes) 64 */ 65 if (PageMappedToDisk(page)) 66 goto mapped; 67 68 /* page is wholly or partially inside EOF */ 69 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) { 70 unsigned offset; 71 offset = i_size_read(inode) & ~PAGE_CACHE_MASK; 72 zero_user_segment(page, offset, PAGE_CACHE_SIZE); 73 } 74 set_page_dirty(page); 75 SetPageUptodate(page); 76 77 trace_f2fs_vm_page_mkwrite(page, DATA); 78 mapped: 79 /* fill the page */ 80 f2fs_wait_on_page_writeback(page, DATA); 81 out: 82 sb_end_pagefault(inode->i_sb); 83 return block_page_mkwrite_return(err); 84 } 85 86 static const struct vm_operations_struct f2fs_file_vm_ops = { 87 .fault = filemap_fault, 88 .map_pages = filemap_map_pages, 89 .page_mkwrite = f2fs_vm_page_mkwrite, 90 .remap_pages = generic_file_remap_pages, 91 }; 92 93 static int get_parent_ino(struct inode *inode, nid_t *pino) 94 { 95 struct dentry *dentry; 96 97 inode = igrab(inode); 98 dentry = d_find_any_alias(inode); 99 iput(inode); 100 if (!dentry) 101 return 0; 102 103 if (update_dent_inode(inode, &dentry->d_name)) { 104 dput(dentry); 105 return 0; 106 } 107 108 *pino = parent_ino(dentry); 109 dput(dentry); 110 return 1; 111 } 112 113 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) 114 { 115 struct inode *inode = file->f_mapping->host; 116 struct f2fs_inode_info *fi = F2FS_I(inode); 117 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 118 int ret = 0; 119 bool need_cp = false; 120 struct writeback_control wbc = { 121 .sync_mode = WB_SYNC_ALL, 122 .nr_to_write = LONG_MAX, 123 .for_reclaim = 0, 124 }; 125 126 if (unlikely(f2fs_readonly(inode->i_sb))) 127 return 0; 128 129 trace_f2fs_sync_file_enter(inode); 130 131 /* if fdatasync is triggered, let's do in-place-update */ 132 if (datasync) 133 set_inode_flag(fi, FI_NEED_IPU); 134 135 ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 136 if (datasync) 137 clear_inode_flag(fi, FI_NEED_IPU); 138 if (ret) { 139 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); 140 return ret; 141 } 142 143 /* 144 * if there is no written data, don't waste time to write recovery info. 145 */ 146 if (!is_inode_flag_set(fi, FI_APPEND_WRITE) && 147 !exist_written_data(sbi, inode->i_ino, APPEND_INO)) { 148 if (is_inode_flag_set(fi, FI_UPDATE_WRITE) || 149 exist_written_data(sbi, inode->i_ino, UPDATE_INO)) 150 goto flush_out; 151 goto out; 152 } 153 154 /* guarantee free sections for fsync */ 155 f2fs_balance_fs(sbi); 156 157 down_read(&fi->i_sem); 158 159 /* 160 * Both of fdatasync() and fsync() are able to be recovered from 161 * sudden-power-off. 162 */ 163 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1) 164 need_cp = true; 165 else if (file_wrong_pino(inode)) 166 need_cp = true; 167 else if (!space_for_roll_forward(sbi)) 168 need_cp = true; 169 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino)) 170 need_cp = true; 171 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi))) 172 need_cp = true; 173 174 up_read(&fi->i_sem); 175 176 if (need_cp) { 177 nid_t pino; 178 179 /* all the dirty node pages should be flushed for POR */ 180 ret = f2fs_sync_fs(inode->i_sb, 1); 181 182 down_write(&fi->i_sem); 183 F2FS_I(inode)->xattr_ver = 0; 184 if (file_wrong_pino(inode) && inode->i_nlink == 1 && 185 get_parent_ino(inode, &pino)) { 186 F2FS_I(inode)->i_pino = pino; 187 file_got_pino(inode); 188 up_write(&fi->i_sem); 189 mark_inode_dirty_sync(inode); 190 ret = f2fs_write_inode(inode, NULL); 191 if (ret) 192 goto out; 193 } else { 194 up_write(&fi->i_sem); 195 } 196 } else { 197 /* if there is no written node page, write its inode page */ 198 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) { 199 if (fsync_mark_done(sbi, inode->i_ino)) 200 goto out; 201 mark_inode_dirty_sync(inode); 202 ret = f2fs_write_inode(inode, NULL); 203 if (ret) 204 goto out; 205 } 206 ret = wait_on_node_pages_writeback(sbi, inode->i_ino); 207 if (ret) 208 goto out; 209 210 /* once recovery info is written, don't need to tack this */ 211 remove_dirty_inode(sbi, inode->i_ino, APPEND_INO); 212 clear_inode_flag(fi, FI_APPEND_WRITE); 213 flush_out: 214 remove_dirty_inode(sbi, inode->i_ino, UPDATE_INO); 215 clear_inode_flag(fi, FI_UPDATE_WRITE); 216 ret = f2fs_issue_flush(F2FS_SB(inode->i_sb)); 217 } 218 out: 219 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); 220 return ret; 221 } 222 223 static pgoff_t __get_first_dirty_index(struct address_space *mapping, 224 pgoff_t pgofs, int whence) 225 { 226 struct pagevec pvec; 227 int nr_pages; 228 229 if (whence != SEEK_DATA) 230 return 0; 231 232 /* find first dirty page index */ 233 pagevec_init(&pvec, 0); 234 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs, 235 PAGECACHE_TAG_DIRTY, 1); 236 pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX; 237 pagevec_release(&pvec); 238 return pgofs; 239 } 240 241 static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs, 242 int whence) 243 { 244 switch (whence) { 245 case SEEK_DATA: 246 if ((blkaddr == NEW_ADDR && dirty == pgofs) || 247 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR)) 248 return true; 249 break; 250 case SEEK_HOLE: 251 if (blkaddr == NULL_ADDR) 252 return true; 253 break; 254 } 255 return false; 256 } 257 258 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) 259 { 260 struct inode *inode = file->f_mapping->host; 261 loff_t maxbytes = inode->i_sb->s_maxbytes; 262 struct dnode_of_data dn; 263 pgoff_t pgofs, end_offset, dirty; 264 loff_t data_ofs = offset; 265 loff_t isize; 266 int err = 0; 267 268 mutex_lock(&inode->i_mutex); 269 270 isize = i_size_read(inode); 271 if (offset >= isize) 272 goto fail; 273 274 /* handle inline data case */ 275 if (f2fs_has_inline_data(inode)) { 276 if (whence == SEEK_HOLE) 277 data_ofs = isize; 278 goto found; 279 } 280 281 pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT); 282 283 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence); 284 285 for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) { 286 set_new_dnode(&dn, inode, NULL, NULL, 0); 287 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA); 288 if (err && err != -ENOENT) { 289 goto fail; 290 } else if (err == -ENOENT) { 291 /* direct node is not exist */ 292 if (whence == SEEK_DATA) { 293 pgofs = PGOFS_OF_NEXT_DNODE(pgofs, 294 F2FS_I(inode)); 295 continue; 296 } else { 297 goto found; 298 } 299 } 300 301 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); 302 303 /* find data/hole in dnode block */ 304 for (; dn.ofs_in_node < end_offset; 305 dn.ofs_in_node++, pgofs++, 306 data_ofs = pgofs << PAGE_CACHE_SHIFT) { 307 block_t blkaddr; 308 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); 309 310 if (__found_offset(blkaddr, dirty, pgofs, whence)) { 311 f2fs_put_dnode(&dn); 312 goto found; 313 } 314 } 315 f2fs_put_dnode(&dn); 316 } 317 318 if (whence == SEEK_DATA) 319 goto fail; 320 found: 321 if (whence == SEEK_HOLE && data_ofs > isize) 322 data_ofs = isize; 323 mutex_unlock(&inode->i_mutex); 324 return vfs_setpos(file, data_ofs, maxbytes); 325 fail: 326 mutex_unlock(&inode->i_mutex); 327 return -ENXIO; 328 } 329 330 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence) 331 { 332 struct inode *inode = file->f_mapping->host; 333 loff_t maxbytes = inode->i_sb->s_maxbytes; 334 335 switch (whence) { 336 case SEEK_SET: 337 case SEEK_CUR: 338 case SEEK_END: 339 return generic_file_llseek_size(file, offset, whence, 340 maxbytes, i_size_read(inode)); 341 case SEEK_DATA: 342 case SEEK_HOLE: 343 return f2fs_seek_block(file, offset, whence); 344 } 345 346 return -EINVAL; 347 } 348 349 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) 350 { 351 file_accessed(file); 352 vma->vm_ops = &f2fs_file_vm_ops; 353 return 0; 354 } 355 356 int truncate_data_blocks_range(struct dnode_of_data *dn, int count) 357 { 358 int nr_free = 0, ofs = dn->ofs_in_node; 359 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); 360 struct f2fs_node *raw_node; 361 __le32 *addr; 362 363 raw_node = F2FS_NODE(dn->node_page); 364 addr = blkaddr_in_node(raw_node) + ofs; 365 366 for (; count > 0; count--, addr++, dn->ofs_in_node++) { 367 block_t blkaddr = le32_to_cpu(*addr); 368 if (blkaddr == NULL_ADDR) 369 continue; 370 371 update_extent_cache(NULL_ADDR, dn); 372 invalidate_blocks(sbi, blkaddr); 373 nr_free++; 374 } 375 if (nr_free) { 376 dec_valid_block_count(sbi, dn->inode, nr_free); 377 set_page_dirty(dn->node_page); 378 sync_inode_page(dn); 379 } 380 dn->ofs_in_node = ofs; 381 382 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid, 383 dn->ofs_in_node, nr_free); 384 return nr_free; 385 } 386 387 void truncate_data_blocks(struct dnode_of_data *dn) 388 { 389 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK); 390 } 391 392 static void truncate_partial_data_page(struct inode *inode, u64 from) 393 { 394 unsigned offset = from & (PAGE_CACHE_SIZE - 1); 395 struct page *page; 396 397 if (f2fs_has_inline_data(inode)) 398 return truncate_inline_data(inode, from); 399 400 if (!offset) 401 return; 402 403 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false); 404 if (IS_ERR(page)) 405 return; 406 407 lock_page(page); 408 if (unlikely(!PageUptodate(page) || 409 page->mapping != inode->i_mapping)) 410 goto out; 411 412 f2fs_wait_on_page_writeback(page, DATA); 413 zero_user(page, offset, PAGE_CACHE_SIZE - offset); 414 set_page_dirty(page); 415 416 out: 417 f2fs_put_page(page, 1); 418 } 419 420 int truncate_blocks(struct inode *inode, u64 from) 421 { 422 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 423 unsigned int blocksize = inode->i_sb->s_blocksize; 424 struct dnode_of_data dn; 425 pgoff_t free_from; 426 int count = 0, err = 0; 427 428 trace_f2fs_truncate_blocks_enter(inode, from); 429 430 if (f2fs_has_inline_data(inode)) 431 goto done; 432 433 free_from = (pgoff_t) 434 ((from + blocksize - 1) >> (sbi->log_blocksize)); 435 436 f2fs_lock_op(sbi); 437 438 set_new_dnode(&dn, inode, NULL, NULL, 0); 439 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE); 440 if (err) { 441 if (err == -ENOENT) 442 goto free_next; 443 f2fs_unlock_op(sbi); 444 trace_f2fs_truncate_blocks_exit(inode, err); 445 return err; 446 } 447 448 count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); 449 450 count -= dn.ofs_in_node; 451 f2fs_bug_on(count < 0); 452 453 if (dn.ofs_in_node || IS_INODE(dn.node_page)) { 454 truncate_data_blocks_range(&dn, count); 455 free_from += count; 456 } 457 458 f2fs_put_dnode(&dn); 459 free_next: 460 err = truncate_inode_blocks(inode, free_from); 461 f2fs_unlock_op(sbi); 462 done: 463 /* lastly zero out the first data page */ 464 truncate_partial_data_page(inode, from); 465 466 trace_f2fs_truncate_blocks_exit(inode, err); 467 return err; 468 } 469 470 void f2fs_truncate(struct inode *inode) 471 { 472 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 473 S_ISLNK(inode->i_mode))) 474 return; 475 476 trace_f2fs_truncate(inode); 477 478 if (!truncate_blocks(inode, i_size_read(inode))) { 479 inode->i_mtime = inode->i_ctime = CURRENT_TIME; 480 mark_inode_dirty(inode); 481 } 482 } 483 484 int f2fs_getattr(struct vfsmount *mnt, 485 struct dentry *dentry, struct kstat *stat) 486 { 487 struct inode *inode = dentry->d_inode; 488 generic_fillattr(inode, stat); 489 stat->blocks <<= 3; 490 return 0; 491 } 492 493 #ifdef CONFIG_F2FS_FS_POSIX_ACL 494 static void __setattr_copy(struct inode *inode, const struct iattr *attr) 495 { 496 struct f2fs_inode_info *fi = F2FS_I(inode); 497 unsigned int ia_valid = attr->ia_valid; 498 499 if (ia_valid & ATTR_UID) 500 inode->i_uid = attr->ia_uid; 501 if (ia_valid & ATTR_GID) 502 inode->i_gid = attr->ia_gid; 503 if (ia_valid & ATTR_ATIME) 504 inode->i_atime = timespec_trunc(attr->ia_atime, 505 inode->i_sb->s_time_gran); 506 if (ia_valid & ATTR_MTIME) 507 inode->i_mtime = timespec_trunc(attr->ia_mtime, 508 inode->i_sb->s_time_gran); 509 if (ia_valid & ATTR_CTIME) 510 inode->i_ctime = timespec_trunc(attr->ia_ctime, 511 inode->i_sb->s_time_gran); 512 if (ia_valid & ATTR_MODE) { 513 umode_t mode = attr->ia_mode; 514 515 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) 516 mode &= ~S_ISGID; 517 set_acl_inode(fi, mode); 518 } 519 } 520 #else 521 #define __setattr_copy setattr_copy 522 #endif 523 524 int f2fs_setattr(struct dentry *dentry, struct iattr *attr) 525 { 526 struct inode *inode = dentry->d_inode; 527 struct f2fs_inode_info *fi = F2FS_I(inode); 528 int err; 529 530 err = inode_change_ok(inode, attr); 531 if (err) 532 return err; 533 534 if ((attr->ia_valid & ATTR_SIZE) && 535 attr->ia_size != i_size_read(inode)) { 536 err = f2fs_convert_inline_data(inode, attr->ia_size); 537 if (err) 538 return err; 539 540 truncate_setsize(inode, attr->ia_size); 541 f2fs_truncate(inode); 542 f2fs_balance_fs(F2FS_SB(inode->i_sb)); 543 } 544 545 __setattr_copy(inode, attr); 546 547 if (attr->ia_valid & ATTR_MODE) { 548 err = posix_acl_chmod(inode, get_inode_mode(inode)); 549 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) { 550 inode->i_mode = fi->i_acl_mode; 551 clear_inode_flag(fi, FI_ACL_MODE); 552 } 553 } 554 555 mark_inode_dirty(inode); 556 return err; 557 } 558 559 const struct inode_operations f2fs_file_inode_operations = { 560 .getattr = f2fs_getattr, 561 .setattr = f2fs_setattr, 562 .get_acl = f2fs_get_acl, 563 .set_acl = f2fs_set_acl, 564 #ifdef CONFIG_F2FS_FS_XATTR 565 .setxattr = generic_setxattr, 566 .getxattr = generic_getxattr, 567 .listxattr = f2fs_listxattr, 568 .removexattr = generic_removexattr, 569 #endif 570 .fiemap = f2fs_fiemap, 571 }; 572 573 static void fill_zero(struct inode *inode, pgoff_t index, 574 loff_t start, loff_t len) 575 { 576 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 577 struct page *page; 578 579 if (!len) 580 return; 581 582 f2fs_balance_fs(sbi); 583 584 f2fs_lock_op(sbi); 585 page = get_new_data_page(inode, NULL, index, false); 586 f2fs_unlock_op(sbi); 587 588 if (!IS_ERR(page)) { 589 f2fs_wait_on_page_writeback(page, DATA); 590 zero_user(page, start, len); 591 set_page_dirty(page); 592 f2fs_put_page(page, 1); 593 } 594 } 595 596 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) 597 { 598 pgoff_t index; 599 int err; 600 601 for (index = pg_start; index < pg_end; index++) { 602 struct dnode_of_data dn; 603 604 set_new_dnode(&dn, inode, NULL, NULL, 0); 605 err = get_dnode_of_data(&dn, index, LOOKUP_NODE); 606 if (err) { 607 if (err == -ENOENT) 608 continue; 609 return err; 610 } 611 612 if (dn.data_blkaddr != NULL_ADDR) 613 truncate_data_blocks_range(&dn, 1); 614 f2fs_put_dnode(&dn); 615 } 616 return 0; 617 } 618 619 static int punch_hole(struct inode *inode, loff_t offset, loff_t len) 620 { 621 pgoff_t pg_start, pg_end; 622 loff_t off_start, off_end; 623 int ret = 0; 624 625 ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1); 626 if (ret) 627 return ret; 628 629 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; 630 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; 631 632 off_start = offset & (PAGE_CACHE_SIZE - 1); 633 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); 634 635 if (pg_start == pg_end) { 636 fill_zero(inode, pg_start, off_start, 637 off_end - off_start); 638 } else { 639 if (off_start) 640 fill_zero(inode, pg_start++, off_start, 641 PAGE_CACHE_SIZE - off_start); 642 if (off_end) 643 fill_zero(inode, pg_end, 0, off_end); 644 645 if (pg_start < pg_end) { 646 struct address_space *mapping = inode->i_mapping; 647 loff_t blk_start, blk_end; 648 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 649 650 f2fs_balance_fs(sbi); 651 652 blk_start = pg_start << PAGE_CACHE_SHIFT; 653 blk_end = pg_end << PAGE_CACHE_SHIFT; 654 truncate_inode_pages_range(mapping, blk_start, 655 blk_end - 1); 656 657 f2fs_lock_op(sbi); 658 ret = truncate_hole(inode, pg_start, pg_end); 659 f2fs_unlock_op(sbi); 660 } 661 } 662 663 return ret; 664 } 665 666 static int expand_inode_data(struct inode *inode, loff_t offset, 667 loff_t len, int mode) 668 { 669 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 670 pgoff_t index, pg_start, pg_end; 671 loff_t new_size = i_size_read(inode); 672 loff_t off_start, off_end; 673 int ret = 0; 674 675 f2fs_balance_fs(sbi); 676 677 ret = inode_newsize_ok(inode, (len + offset)); 678 if (ret) 679 return ret; 680 681 ret = f2fs_convert_inline_data(inode, offset + len); 682 if (ret) 683 return ret; 684 685 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; 686 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; 687 688 off_start = offset & (PAGE_CACHE_SIZE - 1); 689 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); 690 691 f2fs_lock_op(sbi); 692 693 for (index = pg_start; index <= pg_end; index++) { 694 struct dnode_of_data dn; 695 696 if (index == pg_end && !off_end) 697 goto noalloc; 698 699 set_new_dnode(&dn, inode, NULL, NULL, 0); 700 ret = f2fs_reserve_block(&dn, index); 701 if (ret) 702 break; 703 noalloc: 704 if (pg_start == pg_end) 705 new_size = offset + len; 706 else if (index == pg_start && off_start) 707 new_size = (index + 1) << PAGE_CACHE_SHIFT; 708 else if (index == pg_end) 709 new_size = (index << PAGE_CACHE_SHIFT) + off_end; 710 else 711 new_size += PAGE_CACHE_SIZE; 712 } 713 714 if (!(mode & FALLOC_FL_KEEP_SIZE) && 715 i_size_read(inode) < new_size) { 716 i_size_write(inode, new_size); 717 mark_inode_dirty(inode); 718 update_inode_page(inode); 719 } 720 f2fs_unlock_op(sbi); 721 722 return ret; 723 } 724 725 static long f2fs_fallocate(struct file *file, int mode, 726 loff_t offset, loff_t len) 727 { 728 struct inode *inode = file_inode(file); 729 long ret; 730 731 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 732 return -EOPNOTSUPP; 733 734 mutex_lock(&inode->i_mutex); 735 736 if (mode & FALLOC_FL_PUNCH_HOLE) 737 ret = punch_hole(inode, offset, len); 738 else 739 ret = expand_inode_data(inode, offset, len, mode); 740 741 if (!ret) { 742 inode->i_mtime = inode->i_ctime = CURRENT_TIME; 743 mark_inode_dirty(inode); 744 } 745 746 mutex_unlock(&inode->i_mutex); 747 748 trace_f2fs_fallocate(inode, mode, offset, len, ret); 749 return ret; 750 } 751 752 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL)) 753 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL) 754 755 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags) 756 { 757 if (S_ISDIR(mode)) 758 return flags; 759 else if (S_ISREG(mode)) 760 return flags & F2FS_REG_FLMASK; 761 else 762 return flags & F2FS_OTHER_FLMASK; 763 } 764 765 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 766 { 767 struct inode *inode = file_inode(filp); 768 struct f2fs_inode_info *fi = F2FS_I(inode); 769 unsigned int flags; 770 int ret; 771 772 switch (cmd) { 773 case F2FS_IOC_GETFLAGS: 774 flags = fi->i_flags & FS_FL_USER_VISIBLE; 775 return put_user(flags, (int __user *) arg); 776 case F2FS_IOC_SETFLAGS: 777 { 778 unsigned int oldflags; 779 780 ret = mnt_want_write_file(filp); 781 if (ret) 782 return ret; 783 784 if (!inode_owner_or_capable(inode)) { 785 ret = -EACCES; 786 goto out; 787 } 788 789 if (get_user(flags, (int __user *) arg)) { 790 ret = -EFAULT; 791 goto out; 792 } 793 794 flags = f2fs_mask_flags(inode->i_mode, flags); 795 796 mutex_lock(&inode->i_mutex); 797 798 oldflags = fi->i_flags; 799 800 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 801 if (!capable(CAP_LINUX_IMMUTABLE)) { 802 mutex_unlock(&inode->i_mutex); 803 ret = -EPERM; 804 goto out; 805 } 806 } 807 808 flags = flags & FS_FL_USER_MODIFIABLE; 809 flags |= oldflags & ~FS_FL_USER_MODIFIABLE; 810 fi->i_flags = flags; 811 mutex_unlock(&inode->i_mutex); 812 813 f2fs_set_inode_flags(inode); 814 inode->i_ctime = CURRENT_TIME; 815 mark_inode_dirty(inode); 816 out: 817 mnt_drop_write_file(filp); 818 return ret; 819 } 820 default: 821 return -ENOTTY; 822 } 823 } 824 825 #ifdef CONFIG_COMPAT 826 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 827 { 828 switch (cmd) { 829 case F2FS_IOC32_GETFLAGS: 830 cmd = F2FS_IOC_GETFLAGS; 831 break; 832 case F2FS_IOC32_SETFLAGS: 833 cmd = F2FS_IOC_SETFLAGS; 834 break; 835 default: 836 return -ENOIOCTLCMD; 837 } 838 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 839 } 840 #endif 841 842 const struct file_operations f2fs_file_operations = { 843 .llseek = f2fs_llseek, 844 .read = new_sync_read, 845 .write = new_sync_write, 846 .read_iter = generic_file_read_iter, 847 .write_iter = generic_file_write_iter, 848 .open = generic_file_open, 849 .mmap = f2fs_file_mmap, 850 .fsync = f2fs_sync_file, 851 .fallocate = f2fs_fallocate, 852 .unlocked_ioctl = f2fs_ioctl, 853 #ifdef CONFIG_COMPAT 854 .compat_ioctl = f2fs_compat_ioctl, 855 #endif 856 .splice_read = generic_file_splice_read, 857 .splice_write = iter_file_splice_write, 858 }; 859