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