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