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