diff options
Diffstat (limited to 'fs/f2fs/file.c')
-rw-r--r-- | fs/f2fs/file.c | 114 |
1 files changed, 61 insertions, 53 deletions
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index ea272be62677..b41c3579ea9e 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c | |||
@@ -86,7 +86,7 @@ static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma, | |||
86 | trace_f2fs_vm_page_mkwrite(page, DATA); | 86 | trace_f2fs_vm_page_mkwrite(page, DATA); |
87 | mapped: | 87 | mapped: |
88 | /* fill the page */ | 88 | /* fill the page */ |
89 | f2fs_wait_on_page_writeback(page, DATA); | 89 | f2fs_wait_on_page_writeback(page, DATA, false); |
90 | 90 | ||
91 | /* wait for GCed encrypted page writeback */ | 91 | /* wait for GCed encrypted page writeback */ |
92 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) | 92 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) |
@@ -301,7 +301,7 @@ static pgoff_t __get_first_dirty_index(struct address_space *mapping, | |||
301 | pagevec_init(&pvec, 0); | 301 | pagevec_init(&pvec, 0); |
302 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs, | 302 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs, |
303 | PAGECACHE_TAG_DIRTY, 1); | 303 | PAGECACHE_TAG_DIRTY, 1); |
304 | pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX; | 304 | pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX; |
305 | pagevec_release(&pvec); | 305 | pagevec_release(&pvec); |
306 | return pgofs; | 306 | return pgofs; |
307 | } | 307 | } |
@@ -358,15 +358,14 @@ static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) | |||
358 | } else if (err == -ENOENT) { | 358 | } else if (err == -ENOENT) { |
359 | /* direct node does not exists */ | 359 | /* direct node does not exists */ |
360 | if (whence == SEEK_DATA) { | 360 | if (whence == SEEK_DATA) { |
361 | pgofs = PGOFS_OF_NEXT_DNODE(pgofs, | 361 | pgofs = get_next_page_offset(&dn, pgofs); |
362 | F2FS_I(inode)); | ||
363 | continue; | 362 | continue; |
364 | } else { | 363 | } else { |
365 | goto found; | 364 | goto found; |
366 | } | 365 | } |
367 | } | 366 | } |
368 | 367 | ||
369 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); | 368 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
370 | 369 | ||
371 | /* find data/hole in dnode block */ | 370 | /* find data/hole in dnode block */ |
372 | for (; dn.ofs_in_node < end_offset; | 371 | for (; dn.ofs_in_node < end_offset; |
@@ -422,9 +421,11 @@ static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) | |||
422 | int err; | 421 | int err; |
423 | 422 | ||
424 | if (f2fs_encrypted_inode(inode)) { | 423 | if (f2fs_encrypted_inode(inode)) { |
425 | err = f2fs_get_encryption_info(inode); | 424 | err = fscrypt_get_encryption_info(inode); |
426 | if (err) | 425 | if (err) |
427 | return 0; | 426 | return 0; |
427 | if (!f2fs_encrypted_inode(inode)) | ||
428 | return -ENOKEY; | ||
428 | } | 429 | } |
429 | 430 | ||
430 | /* we don't need to use inline_data strictly */ | 431 | /* we don't need to use inline_data strictly */ |
@@ -440,12 +441,18 @@ static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) | |||
440 | static int f2fs_file_open(struct inode *inode, struct file *filp) | 441 | static int f2fs_file_open(struct inode *inode, struct file *filp) |
441 | { | 442 | { |
442 | int ret = generic_file_open(inode, filp); | 443 | int ret = generic_file_open(inode, filp); |
444 | struct inode *dir = filp->f_path.dentry->d_parent->d_inode; | ||
443 | 445 | ||
444 | if (!ret && f2fs_encrypted_inode(inode)) { | 446 | if (!ret && f2fs_encrypted_inode(inode)) { |
445 | ret = f2fs_get_encryption_info(inode); | 447 | ret = fscrypt_get_encryption_info(inode); |
446 | if (ret) | 448 | if (ret) |
447 | ret = -EACCES; | 449 | return -EACCES; |
450 | if (!fscrypt_has_encryption_key(inode)) | ||
451 | return -ENOKEY; | ||
448 | } | 452 | } |
453 | if (f2fs_encrypted_inode(dir) && | ||
454 | !fscrypt_has_permitted_context(dir, inode)) | ||
455 | return -EPERM; | ||
449 | return ret; | 456 | return ret; |
450 | } | 457 | } |
451 | 458 | ||
@@ -480,7 +487,7 @@ int truncate_data_blocks_range(struct dnode_of_data *dn, int count) | |||
480 | * we will invalidate all blkaddr in the whole range. | 487 | * we will invalidate all blkaddr in the whole range. |
481 | */ | 488 | */ |
482 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), | 489 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), |
483 | F2FS_I(dn->inode)) + ofs; | 490 | dn->inode) + ofs; |
484 | f2fs_update_extent_cache_range(dn, fofs, 0, len); | 491 | f2fs_update_extent_cache_range(dn, fofs, 0, len); |
485 | dec_valid_block_count(sbi, dn->inode, nr_free); | 492 | dec_valid_block_count(sbi, dn->inode, nr_free); |
486 | sync_inode_page(dn); | 493 | sync_inode_page(dn); |
@@ -521,9 +528,10 @@ static int truncate_partial_data_page(struct inode *inode, u64 from, | |||
521 | if (IS_ERR(page)) | 528 | if (IS_ERR(page)) |
522 | return 0; | 529 | return 0; |
523 | truncate_out: | 530 | truncate_out: |
524 | f2fs_wait_on_page_writeback(page, DATA); | 531 | f2fs_wait_on_page_writeback(page, DATA, true); |
525 | zero_user(page, offset, PAGE_CACHE_SIZE - offset); | 532 | zero_user(page, offset, PAGE_CACHE_SIZE - offset); |
526 | if (!cache_only || !f2fs_encrypted_inode(inode) || !S_ISREG(inode->i_mode)) | 533 | if (!cache_only || !f2fs_encrypted_inode(inode) || |
534 | !S_ISREG(inode->i_mode)) | ||
527 | set_page_dirty(page); | 535 | set_page_dirty(page); |
528 | f2fs_put_page(page, 1); | 536 | f2fs_put_page(page, 1); |
529 | return 0; | 537 | return 0; |
@@ -568,7 +576,7 @@ int truncate_blocks(struct inode *inode, u64 from, bool lock) | |||
568 | goto out; | 576 | goto out; |
569 | } | 577 | } |
570 | 578 | ||
571 | count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); | 579 | count = ADDRS_PER_PAGE(dn.node_page, inode); |
572 | 580 | ||
573 | count -= dn.ofs_in_node; | 581 | count -= dn.ofs_in_node; |
574 | f2fs_bug_on(sbi, count < 0); | 582 | f2fs_bug_on(sbi, count < 0); |
@@ -671,7 +679,7 @@ int f2fs_setattr(struct dentry *dentry, struct iattr *attr) | |||
671 | 679 | ||
672 | if (attr->ia_valid & ATTR_SIZE) { | 680 | if (attr->ia_valid & ATTR_SIZE) { |
673 | if (f2fs_encrypted_inode(inode) && | 681 | if (f2fs_encrypted_inode(inode) && |
674 | f2fs_get_encryption_info(inode)) | 682 | fscrypt_get_encryption_info(inode)) |
675 | return -EACCES; | 683 | return -EACCES; |
676 | 684 | ||
677 | if (attr->ia_size <= i_size_read(inode)) { | 685 | if (attr->ia_size <= i_size_read(inode)) { |
@@ -743,7 +751,7 @@ static int fill_zero(struct inode *inode, pgoff_t index, | |||
743 | if (IS_ERR(page)) | 751 | if (IS_ERR(page)) |
744 | return PTR_ERR(page); | 752 | return PTR_ERR(page); |
745 | 753 | ||
746 | f2fs_wait_on_page_writeback(page, DATA); | 754 | f2fs_wait_on_page_writeback(page, DATA, true); |
747 | zero_user(page, start, len); | 755 | zero_user(page, start, len); |
748 | set_page_dirty(page); | 756 | set_page_dirty(page); |
749 | f2fs_put_page(page, 1); | 757 | f2fs_put_page(page, 1); |
@@ -768,7 +776,7 @@ int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) | |||
768 | return err; | 776 | return err; |
769 | } | 777 | } |
770 | 778 | ||
771 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); | 779 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
772 | count = min(end_offset - dn.ofs_in_node, pg_end - pg_start); | 780 | count = min(end_offset - dn.ofs_in_node, pg_end - pg_start); |
773 | 781 | ||
774 | f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset); | 782 | f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset); |
@@ -854,10 +862,8 @@ static int __exchange_data_block(struct inode *inode, pgoff_t src, | |||
854 | } else { | 862 | } else { |
855 | new_addr = dn.data_blkaddr; | 863 | new_addr = dn.data_blkaddr; |
856 | if (!is_checkpointed_data(sbi, new_addr)) { | 864 | if (!is_checkpointed_data(sbi, new_addr)) { |
857 | dn.data_blkaddr = NULL_ADDR; | ||
858 | /* do not invalidate this block address */ | 865 | /* do not invalidate this block address */ |
859 | set_data_blkaddr(&dn); | 866 | f2fs_update_data_blkaddr(&dn, NULL_ADDR); |
860 | f2fs_update_extent_cache(&dn); | ||
861 | do_replace = true; | 867 | do_replace = true; |
862 | } | 868 | } |
863 | f2fs_put_dnode(&dn); | 869 | f2fs_put_dnode(&dn); |
@@ -884,7 +890,7 @@ static int __exchange_data_block(struct inode *inode, pgoff_t src, | |||
884 | 890 | ||
885 | get_node_info(sbi, dn.nid, &ni); | 891 | get_node_info(sbi, dn.nid, &ni); |
886 | f2fs_replace_block(sbi, &dn, dn.data_blkaddr, new_addr, | 892 | f2fs_replace_block(sbi, &dn, dn.data_blkaddr, new_addr, |
887 | ni.version, true); | 893 | ni.version, true, false); |
888 | f2fs_put_dnode(&dn); | 894 | f2fs_put_dnode(&dn); |
889 | } else { | 895 | } else { |
890 | struct page *psrc, *pdst; | 896 | struct page *psrc, *pdst; |
@@ -892,7 +898,7 @@ static int __exchange_data_block(struct inode *inode, pgoff_t src, | |||
892 | psrc = get_lock_data_page(inode, src, true); | 898 | psrc = get_lock_data_page(inode, src, true); |
893 | if (IS_ERR(psrc)) | 899 | if (IS_ERR(psrc)) |
894 | return PTR_ERR(psrc); | 900 | return PTR_ERR(psrc); |
895 | pdst = get_new_data_page(inode, NULL, dst, false); | 901 | pdst = get_new_data_page(inode, NULL, dst, true); |
896 | if (IS_ERR(pdst)) { | 902 | if (IS_ERR(pdst)) { |
897 | f2fs_put_page(psrc, 1); | 903 | f2fs_put_page(psrc, 1); |
898 | return PTR_ERR(pdst); | 904 | return PTR_ERR(pdst); |
@@ -908,9 +914,7 @@ static int __exchange_data_block(struct inode *inode, pgoff_t src, | |||
908 | 914 | ||
909 | err_out: | 915 | err_out: |
910 | if (!get_dnode_of_data(&dn, src, LOOKUP_NODE)) { | 916 | if (!get_dnode_of_data(&dn, src, LOOKUP_NODE)) { |
911 | dn.data_blkaddr = new_addr; | 917 | f2fs_update_data_blkaddr(&dn, new_addr); |
912 | set_data_blkaddr(&dn); | ||
913 | f2fs_update_extent_cache(&dn); | ||
914 | f2fs_put_dnode(&dn); | 918 | f2fs_put_dnode(&dn); |
915 | } | 919 | } |
916 | return ret; | 920 | return ret; |
@@ -1050,12 +1054,7 @@ static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len, | |||
1050 | 1054 | ||
1051 | if (dn.data_blkaddr != NEW_ADDR) { | 1055 | if (dn.data_blkaddr != NEW_ADDR) { |
1052 | invalidate_blocks(sbi, dn.data_blkaddr); | 1056 | invalidate_blocks(sbi, dn.data_blkaddr); |
1053 | 1057 | f2fs_update_data_blkaddr(&dn, NEW_ADDR); | |
1054 | dn.data_blkaddr = NEW_ADDR; | ||
1055 | set_data_blkaddr(&dn); | ||
1056 | |||
1057 | dn.data_blkaddr = NULL_ADDR; | ||
1058 | f2fs_update_extent_cache(&dn); | ||
1059 | } | 1058 | } |
1060 | f2fs_put_dnode(&dn); | 1059 | f2fs_put_dnode(&dn); |
1061 | f2fs_unlock_op(sbi); | 1060 | f2fs_unlock_op(sbi); |
@@ -1253,7 +1252,7 @@ static int f2fs_release_file(struct inode *inode, struct file *filp) | |||
1253 | { | 1252 | { |
1254 | /* some remained atomic pages should discarded */ | 1253 | /* some remained atomic pages should discarded */ |
1255 | if (f2fs_is_atomic_file(inode)) | 1254 | if (f2fs_is_atomic_file(inode)) |
1256 | commit_inmem_pages(inode, true); | 1255 | drop_inmem_pages(inode); |
1257 | if (f2fs_is_volatile_file(inode)) { | 1256 | if (f2fs_is_volatile_file(inode)) { |
1258 | set_inode_flag(F2FS_I(inode), FI_DROP_CACHE); | 1257 | set_inode_flag(F2FS_I(inode), FI_DROP_CACHE); |
1259 | filemap_fdatawrite(inode->i_mapping); | 1258 | filemap_fdatawrite(inode->i_mapping); |
@@ -1377,7 +1376,7 @@ static int f2fs_ioc_commit_atomic_write(struct file *filp) | |||
1377 | 1376 | ||
1378 | if (f2fs_is_atomic_file(inode)) { | 1377 | if (f2fs_is_atomic_file(inode)) { |
1379 | clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); | 1378 | clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); |
1380 | ret = commit_inmem_pages(inode, false); | 1379 | ret = commit_inmem_pages(inode); |
1381 | if (ret) { | 1380 | if (ret) { |
1382 | set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); | 1381 | set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); |
1383 | goto err_out; | 1382 | goto err_out; |
@@ -1440,7 +1439,7 @@ static int f2fs_ioc_abort_volatile_write(struct file *filp) | |||
1440 | 1439 | ||
1441 | if (f2fs_is_atomic_file(inode)) { | 1440 | if (f2fs_is_atomic_file(inode)) { |
1442 | clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); | 1441 | clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); |
1443 | commit_inmem_pages(inode, true); | 1442 | drop_inmem_pages(inode); |
1444 | } | 1443 | } |
1445 | if (f2fs_is_volatile_file(inode)) { | 1444 | if (f2fs_is_volatile_file(inode)) { |
1446 | clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE); | 1445 | clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE); |
@@ -1535,39 +1534,30 @@ static bool uuid_is_nonzero(__u8 u[16]) | |||
1535 | 1534 | ||
1536 | static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg) | 1535 | static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg) |
1537 | { | 1536 | { |
1538 | #ifdef CONFIG_F2FS_FS_ENCRYPTION | 1537 | struct fscrypt_policy policy; |
1539 | struct f2fs_encryption_policy policy; | ||
1540 | struct inode *inode = file_inode(filp); | 1538 | struct inode *inode = file_inode(filp); |
1541 | 1539 | ||
1542 | if (copy_from_user(&policy, (struct f2fs_encryption_policy __user *)arg, | 1540 | if (copy_from_user(&policy, (struct fscrypt_policy __user *)arg, |
1543 | sizeof(policy))) | 1541 | sizeof(policy))) |
1544 | return -EFAULT; | 1542 | return -EFAULT; |
1545 | 1543 | ||
1546 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); | 1544 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
1547 | return f2fs_process_policy(&policy, inode); | 1545 | return fscrypt_process_policy(inode, &policy); |
1548 | #else | ||
1549 | return -EOPNOTSUPP; | ||
1550 | #endif | ||
1551 | } | 1546 | } |
1552 | 1547 | ||
1553 | static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg) | 1548 | static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg) |
1554 | { | 1549 | { |
1555 | #ifdef CONFIG_F2FS_FS_ENCRYPTION | 1550 | struct fscrypt_policy policy; |
1556 | struct f2fs_encryption_policy policy; | ||
1557 | struct inode *inode = file_inode(filp); | 1551 | struct inode *inode = file_inode(filp); |
1558 | int err; | 1552 | int err; |
1559 | 1553 | ||
1560 | err = f2fs_get_policy(inode, &policy); | 1554 | err = fscrypt_get_policy(inode, &policy); |
1561 | if (err) | 1555 | if (err) |
1562 | return err; | 1556 | return err; |
1563 | 1557 | ||
1564 | if (copy_to_user((struct f2fs_encryption_policy __user *)arg, &policy, | 1558 | if (copy_to_user((struct fscrypt_policy __user *)arg, &policy, sizeof(policy))) |
1565 | sizeof(policy))) | ||
1566 | return -EFAULT; | 1559 | return -EFAULT; |
1567 | return 0; | 1560 | return 0; |
1568 | #else | ||
1569 | return -EOPNOTSUPP; | ||
1570 | #endif | ||
1571 | } | 1561 | } |
1572 | 1562 | ||
1573 | static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg) | 1563 | static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg) |
@@ -1648,7 +1638,7 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi, | |||
1648 | struct f2fs_defragment *range) | 1638 | struct f2fs_defragment *range) |
1649 | { | 1639 | { |
1650 | struct inode *inode = file_inode(filp); | 1640 | struct inode *inode = file_inode(filp); |
1651 | struct f2fs_map_blocks map; | 1641 | struct f2fs_map_blocks map = { .m_next_pgofs = NULL }; |
1652 | struct extent_info ei; | 1642 | struct extent_info ei; |
1653 | pgoff_t pg_start, pg_end; | 1643 | pgoff_t pg_start, pg_end; |
1654 | unsigned int blk_per_seg = sbi->blocks_per_seg; | 1644 | unsigned int blk_per_seg = sbi->blocks_per_seg; |
@@ -1874,14 +1864,32 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |||
1874 | 1864 | ||
1875 | static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) | 1865 | static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
1876 | { | 1866 | { |
1877 | struct inode *inode = file_inode(iocb->ki_filp); | 1867 | struct file *file = iocb->ki_filp; |
1868 | struct inode *inode = file_inode(file); | ||
1869 | ssize_t ret; | ||
1878 | 1870 | ||
1879 | if (f2fs_encrypted_inode(inode) && | 1871 | if (f2fs_encrypted_inode(inode) && |
1880 | !f2fs_has_encryption_key(inode) && | 1872 | !fscrypt_has_encryption_key(inode) && |
1881 | f2fs_get_encryption_info(inode)) | 1873 | fscrypt_get_encryption_info(inode)) |
1882 | return -EACCES; | 1874 | return -EACCES; |
1883 | 1875 | ||
1884 | return generic_file_write_iter(iocb, from); | 1876 | inode_lock(inode); |
1877 | ret = generic_write_checks(iocb, from); | ||
1878 | if (ret > 0) { | ||
1879 | ret = f2fs_preallocate_blocks(iocb, from); | ||
1880 | if (!ret) | ||
1881 | ret = __generic_file_write_iter(iocb, from); | ||
1882 | } | ||
1883 | inode_unlock(inode); | ||
1884 | |||
1885 | if (ret > 0) { | ||
1886 | ssize_t err; | ||
1887 | |||
1888 | err = generic_write_sync(file, iocb->ki_pos - ret, ret); | ||
1889 | if (err < 0) | ||
1890 | ret = err; | ||
1891 | } | ||
1892 | return ret; | ||
1885 | } | 1893 | } |
1886 | 1894 | ||
1887 | #ifdef CONFIG_COMPAT | 1895 | #ifdef CONFIG_COMPAT |