aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaegeuk Kim2020-12-28 16:30:20 -0600
committerJaegeuk Kim2020-12-28 16:33:39 -0600
commit372f5972b14c1acbc11f8e76da4090a247f2e015 (patch)
treeed5344fa475478fe2961b94e3905ae744b103789
parent8c1b0c9acefe044aa53631be8635cd3b7661203b (diff)
parent3ed3a9407a87a22947ccea4ab7e65c9826cafd22 (diff)
downloadkernel-372f5972b14c1acbc11f8e76da4090a247f2e015.tar.gz
kernel-372f5972b14c1acbc11f8e76da4090a247f2e015.tar.xz
kernel-372f5972b14c1acbc11f8e76da4090a247f2e015.zip
Merge remote-tracking branch 'aosp/upstream-f2fs-stable-linux-5.10.y' into android12-5.10
* aosp/upstream-f2fs-stable-linux-5.10.y: f2fs: compress: fix compression chksum f2fs: fix shift-out-of-bounds in sanity_check_raw_super() f2fs: fix race of pending_pages in decompression f2fs: fix to account inline xattr correctly during recovery f2fs: inline: fix wrong inline inode stat f2fs: inline: correct comment in f2fs_recover_inline_data f2fs: don't check PAGE_SIZE again in sanity_check_raw_super() f2fs: convert to F2FS_*_INO macro f2fs: introduce max_io_bytes, a sysfs entry, to limit bio size f2fs: don't allow any writes on readonly mount f2fs: avoid race condition for shrinker count f2fs: add F2FS_IOC_DECOMPRESS_FILE and F2FS_IOC_COMPRESS_FILE f2fs: add compress_mode mount option f2fs: Remove unnecessary unlikely() f2fs: init dirty_secmap incorrectly f2fs: remove buffer_head which has 32bits limit f2fs: fix wrong block count instead of bytes f2fs: use new conversion functions between blks and bytes f2fs: rename logical_to_blk and blk_to_logical f2fs: fix kbytes written stat for multi-device case f2fs: compress: support chksum f2fs: fix to avoid REQ_TIME and CP_TIME collision f2fs: change to use rwsem for cp_mutex f2fs: Handle casefolding with Encryption fscrypt: Have filesystems handle their d_ops libfs: Add generic function for setting dentry_ops f2fs: Remove the redundancy initialization f2fs: remove writeback_inodes_sb in f2fs_remount f2fs: fix double free of unicode map f2fs: fix compat F2FS_IOC_{MOVE,GARBAGE_COLLECT}_RANGE f2fs: avoid unneeded data copy in f2fs_ioc_move_range() f2fs: add F2FS_IOC_SET_COMPRESS_OPTION ioctl f2fs: add F2FS_IOC_GET_COMPRESS_OPTION ioctl f2fs: move ioctl interface definitions to separated file f2fs: fix to seek incorrect data offset in inline data file f2fs: call f2fs_get_meta_page_retry for nat page Bug: 174873661 Signed-off-by: Jaegeuk Kim <jaegeuk@google.com> Change-Id: I1edcbc31855a7b9d5fd5623ad0fd420e4885eaf2
-rw-r--r--Documentation/ABI/testing/sysfs-fs-f2fs7
-rw-r--r--Documentation/filesystems/f2fs.rst36
-rw-r--r--fs/f2fs/acl.c2
-rw-r--r--fs/f2fs/checkpoint.c39
-rw-r--r--fs/f2fs/compress.c24
-rw-r--r--fs/f2fs/compress.h0
-rw-r--r--fs/f2fs/data.c153
-rw-r--r--fs/f2fs/debug.c11
-rw-r--r--fs/f2fs/f2fs.h169
-rw-r--r--fs/f2fs/file.c411
-rw-r--r--fs/f2fs/gc.c4
-rw-r--r--fs/f2fs/inline.c7
-rw-r--r--fs/f2fs/inode.c3
-rw-r--r--fs/f2fs/node.c41
-rw-r--r--fs/f2fs/node.h4
-rw-r--r--fs/f2fs/recovery.c4
-rw-r--r--fs/f2fs/segment.c49
-rw-r--r--fs/f2fs/shrinker.c4
-rw-r--r--fs/f2fs/super.c63
-rw-r--r--fs/f2fs/sysfs.c5
-rw-r--r--include/linux/f2fs_fs.h2
-rw-r--r--include/trace/events/f2fs.h1
-rw-r--r--include/uapi/linux/f2fs.h98
23 files changed, 832 insertions, 305 deletions
diff --git a/Documentation/ABI/testing/sysfs-fs-f2fs b/Documentation/ABI/testing/sysfs-fs-f2fs
index 67b3ed8e8c2f..3dfee94e0618 100644
--- a/Documentation/ABI/testing/sysfs-fs-f2fs
+++ b/Documentation/ABI/testing/sysfs-fs-f2fs
@@ -370,3 +370,10 @@ Date: April 2020
370Contact: "Daeho Jeong" <daehojeong@google.com> 370Contact: "Daeho Jeong" <daehojeong@google.com>
371Description: Give a way to change iostat_period time. 3secs by default. 371Description: Give a way to change iostat_period time. 3secs by default.
372 The new iostat trace gives stats gap given the period. 372 The new iostat trace gives stats gap given the period.
373What: /sys/fs/f2fs/<disk>/max_io_bytes
374Date: December 2020
375Contact: "Jaegeuk Kim" <jaegeuk@kernel.org>
376Description: This gives a control to limit the bio size in f2fs.
377 Default is zero, which will follow underlying block layer limit,
378 whereas, if it has a certain bytes value, f2fs won't submit a
379 bio larger than that size.
diff --git a/Documentation/filesystems/f2fs.rst b/Documentation/filesystems/f2fs.rst
index b8ee761c9922..dae15c96e659 100644
--- a/Documentation/filesystems/f2fs.rst
+++ b/Documentation/filesystems/f2fs.rst
@@ -260,6 +260,14 @@ compress_extension=%s Support adding specified extension, so that f2fs can enab
260 For other files, we can still enable compression via ioctl. 260 For other files, we can still enable compression via ioctl.
261 Note that, there is one reserved special extension '*', it 261 Note that, there is one reserved special extension '*', it
262 can be set to enable compression for all files. 262 can be set to enable compression for all files.
263compress_chksum Support verifying chksum of raw data in compressed cluster.
264compress_mode=%s Control file compression mode. This supports "fs" and "user"
265 modes. In "fs" mode (default), f2fs does automatic compression
266 on the compression enabled files. In "user" mode, f2fs disables
267 the automaic compression and gives the user discretion of
268 choosing the target file and the timing. The user can do manual
269 compression/decompression on the compression enabled files using
270 ioctls.
263inlinecrypt When possible, encrypt/decrypt the contents of encrypted 271inlinecrypt When possible, encrypt/decrypt the contents of encrypted
264 files using the blk-crypto framework rather than 272 files using the blk-crypto framework rather than
265 filesystem-layer encryption. This allows the use of 273 filesystem-layer encryption. This allows the use of
@@ -810,6 +818,34 @@ Compress metadata layout::
810 | data length | data chksum | reserved | compressed data | 818 | data length | data chksum | reserved | compressed data |
811 +-------------+-------------+----------+----------------------------+ 819 +-------------+-------------+----------+----------------------------+
812 820
821Compression mode
822--------------------------
823
824f2fs supports "fs" and "user" compression modes with "compression_mode" mount option.
825With this option, f2fs provides a choice to select the way how to compress the
826compression enabled files (refer to "Compression implementation" section for how to
827enable compression on a regular inode).
828
8291) compress_mode=fs
830This is the default option. f2fs does automatic compression in the writeback of the
831compression enabled files.
832
8332) compress_mode=user
834This disables the automaic compression and gives the user discretion of choosing the
835target file and the timing. The user can do manual compression/decompression on the
836compression enabled files using F2FS_IOC_DECOMPRESS_FILE and F2FS_IOC_COMPRESS_FILE
837ioctls like the below.
838
839To decompress a file,
840
841fd = open(filename, O_WRONLY, 0);
842ret = ioctl(fd, F2FS_IOC_DECOMPRESS_FILE);
843
844To compress a file,
845
846fd = open(filename, O_WRONLY, 0);
847ret = ioctl(fd, F2FS_IOC_COMPRESS_FILE);
848
813NVMe Zoned Namespace devices 849NVMe Zoned Namespace devices
814---------------------------- 850----------------------------
815 851
diff --git a/fs/f2fs/acl.c b/fs/f2fs/acl.c
index 306413589827..1e5e9b1136ee 100644
--- a/fs/f2fs/acl.c
+++ b/fs/f2fs/acl.c
@@ -384,7 +384,7 @@ int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage,
384 struct page *dpage) 384 struct page *dpage)
385{ 385{
386 struct posix_acl *default_acl = NULL, *acl = NULL; 386 struct posix_acl *default_acl = NULL, *acl = NULL;
387 int error = 0; 387 int error;
388 388
389 error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dpage); 389 error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dpage);
390 if (error) 390 if (error)
diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c
index 023462e80e58..617d0f6b0836 100644
--- a/fs/f2fs/checkpoint.c
+++ b/fs/f2fs/checkpoint.c
@@ -37,7 +37,7 @@ void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
37struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index) 37struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
38{ 38{
39 struct address_space *mapping = META_MAPPING(sbi); 39 struct address_space *mapping = META_MAPPING(sbi);
40 struct page *page = NULL; 40 struct page *page;
41repeat: 41repeat:
42 page = f2fs_grab_cache_page(mapping, index, false); 42 page = f2fs_grab_cache_page(mapping, index, false);
43 if (!page) { 43 if (!page) {
@@ -348,13 +348,13 @@ static int f2fs_write_meta_pages(struct address_space *mapping,
348 goto skip_write; 348 goto skip_write;
349 349
350 /* if locked failed, cp will flush dirty pages instead */ 350 /* if locked failed, cp will flush dirty pages instead */
351 if (!mutex_trylock(&sbi->cp_mutex)) 351 if (!down_write_trylock(&sbi->cp_global_sem))
352 goto skip_write; 352 goto skip_write;
353 353
354 trace_f2fs_writepages(mapping->host, wbc, META); 354 trace_f2fs_writepages(mapping->host, wbc, META);
355 diff = nr_pages_to_write(sbi, META, wbc); 355 diff = nr_pages_to_write(sbi, META, wbc);
356 written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO); 356 written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
357 mutex_unlock(&sbi->cp_mutex); 357 up_write(&sbi->cp_global_sem);
358 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff); 358 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
359 return 0; 359 return 0;
360 360
@@ -1385,6 +1385,27 @@ static void commit_checkpoint(struct f2fs_sb_info *sbi,
1385 f2fs_submit_merged_write(sbi, META_FLUSH); 1385 f2fs_submit_merged_write(sbi, META_FLUSH);
1386} 1386}
1387 1387
1388static inline u64 get_sectors_written(struct block_device *bdev)
1389{
1390 return bdev->bd_part ?
1391 (u64)part_stat_read(bdev->bd_part, sectors[STAT_WRITE]) : 0;
1392}
1393
1394u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi)
1395{
1396 if (f2fs_is_multi_device(sbi)) {
1397 u64 sectors = 0;
1398 int i;
1399
1400 for (i = 0; i < sbi->s_ndevs; i++)
1401 sectors += get_sectors_written(FDEV(i).bdev);
1402
1403 return sectors;
1404 }
1405
1406 return get_sectors_written(sbi->sb->s_bdev);
1407}
1408
1388static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1409static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1389{ 1410{
1390 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1411 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
@@ -1395,7 +1416,6 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1395 __u32 crc32 = 0; 1416 __u32 crc32 = 0;
1396 int i; 1417 int i;
1397 int cp_payload_blks = __cp_payload(sbi); 1418 int cp_payload_blks = __cp_payload(sbi);
1398 struct super_block *sb = sbi->sb;
1399 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 1419 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1400 u64 kbytes_written; 1420 u64 kbytes_written;
1401 int err; 1421 int err;
@@ -1490,9 +1510,8 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1490 1510
1491 /* Record write statistics in the hot node summary */ 1511 /* Record write statistics in the hot node summary */
1492 kbytes_written = sbi->kbytes_written; 1512 kbytes_written = sbi->kbytes_written;
1493 if (sb->s_bdev->bd_part) 1513 kbytes_written += (f2fs_get_sectors_written(sbi) -
1494 kbytes_written += BD_PART_WRITTEN(sbi); 1514 sbi->sectors_written_start) >> 1;
1495
1496 seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written); 1515 seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
1497 1516
1498 if (__remain_node_summaries(cpc->reason)) { 1517 if (__remain_node_summaries(cpc->reason)) {
@@ -1572,7 +1591,7 @@ int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1572 f2fs_warn(sbi, "Start checkpoint disabled!"); 1591 f2fs_warn(sbi, "Start checkpoint disabled!");
1573 } 1592 }
1574 if (cpc->reason != CP_RESIZE) 1593 if (cpc->reason != CP_RESIZE)
1575 mutex_lock(&sbi->cp_mutex); 1594 down_write(&sbi->cp_global_sem);
1576 1595
1577 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) && 1596 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1578 ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) || 1597 ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
@@ -1600,7 +1619,7 @@ int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1600 goto out; 1619 goto out;
1601 } 1620 }
1602 1621
1603 if (NM_I(sbi)->dirty_nat_cnt == 0 && 1622 if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 &&
1604 SIT_I(sbi)->dirty_sentries == 0 && 1623 SIT_I(sbi)->dirty_sentries == 0 &&
1605 prefree_segments(sbi) == 0) { 1624 prefree_segments(sbi) == 0) {
1606 f2fs_flush_sit_entries(sbi, cpc); 1625 f2fs_flush_sit_entries(sbi, cpc);
@@ -1647,7 +1666,7 @@ stop:
1647 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint"); 1666 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
1648out: 1667out:
1649 if (cpc->reason != CP_RESIZE) 1668 if (cpc->reason != CP_RESIZE)
1650 mutex_unlock(&sbi->cp_mutex); 1669 up_write(&sbi->cp_global_sem);
1651 return err; 1670 return err;
1652} 1671}
1653 1672
diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
index c5fee4d7ea72..4bcbacfe3325 100644
--- a/fs/f2fs/compress.c
+++ b/fs/f2fs/compress.c
@@ -602,6 +602,7 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
602 f2fs_cops[fi->i_compress_algorithm]; 602 f2fs_cops[fi->i_compress_algorithm];
603 unsigned int max_len, new_nr_cpages; 603 unsigned int max_len, new_nr_cpages;
604 struct page **new_cpages; 604 struct page **new_cpages;
605 u32 chksum = 0;
605 int i, ret; 606 int i, ret;
606 607
607 trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx, 608 trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
@@ -655,6 +656,11 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
655 656
656 cc->cbuf->clen = cpu_to_le32(cc->clen); 657 cc->cbuf->clen = cpu_to_le32(cc->clen);
657 658
659 if (fi->i_compress_flag & 1 << COMPRESS_CHKSUM)
660 chksum = f2fs_crc32(F2FS_I_SB(cc->inode),
661 cc->cbuf->cdata, cc->clen);
662 cc->cbuf->chksum = cpu_to_le32(chksum);
663
658 for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++) 664 for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
659 cc->cbuf->reserved[i] = cpu_to_le32(0); 665 cc->cbuf->reserved[i] = cpu_to_le32(0);
660 666
@@ -790,6 +796,22 @@ void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity)
790 796
791 ret = cops->decompress_pages(dic); 797 ret = cops->decompress_pages(dic);
792 798
799 if (!ret && (fi->i_compress_flag & 1 << COMPRESS_CHKSUM)) {
800 u32 provided = le32_to_cpu(dic->cbuf->chksum);
801 u32 calculated = f2fs_crc32(sbi, dic->cbuf->cdata, dic->clen);
802
803 if (provided != calculated) {
804 if (!is_inode_flag_set(dic->inode, FI_COMPRESS_CORRUPT)) {
805 set_inode_flag(dic->inode, FI_COMPRESS_CORRUPT);
806 printk_ratelimited(
807 "%sF2FS-fs (%s): checksum invalid, nid = %lu, %x vs %x",
808 KERN_INFO, sbi->sb->s_id, dic->inode->i_ino,
809 provided, calculated);
810 }
811 set_sbi_flag(sbi, SBI_NEED_FSCK);
812 }
813 }
814
793out_vunmap_cbuf: 815out_vunmap_cbuf:
794 vm_unmap_ram(dic->cbuf, dic->nr_cpages); 816 vm_unmap_ram(dic->cbuf, dic->nr_cpages);
795out_vunmap_rbuf: 817out_vunmap_rbuf:
@@ -919,7 +941,7 @@ int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
919 941
920static bool cluster_may_compress(struct compress_ctx *cc) 942static bool cluster_may_compress(struct compress_ctx *cc)
921{ 943{
922 if (!f2fs_compressed_file(cc->inode)) 944 if (!f2fs_need_compress_data(cc->inode))
923 return false; 945 return false;
924 if (f2fs_is_atomic_file(cc->inode)) 946 if (f2fs_is_atomic_file(cc->inode))
925 return false; 947 return false;
diff --git a/fs/f2fs/compress.h b/fs/f2fs/compress.h
new file mode 100644
index 000000000000..e69de29bb2d1
--- /dev/null
+++ b/fs/f2fs/compress.h
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 051a95cdf3a2..d1e83f119338 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -741,6 +741,9 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)
741static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio, 741static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
742 block_t last_blkaddr, block_t cur_blkaddr) 742 block_t last_blkaddr, block_t cur_blkaddr)
743{ 743{
744 if (unlikely(sbi->max_io_bytes &&
745 bio->bi_iter.bi_size >= sbi->max_io_bytes))
746 return false;
744 if (last_blkaddr + 1 != cur_blkaddr) 747 if (last_blkaddr + 1 != cur_blkaddr)
745 return false; 748 return false;
746 return __same_bdev(sbi, cur_blkaddr, bio); 749 return __same_bdev(sbi, cur_blkaddr, bio);
@@ -1756,6 +1759,16 @@ bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1756 return true; 1759 return true;
1757} 1760}
1758 1761
1762static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1763{
1764 return (bytes >> inode->i_blkbits);
1765}
1766
1767static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1768{
1769 return (blks << inode->i_blkbits);
1770}
1771
1759static int __get_data_block(struct inode *inode, sector_t iblock, 1772static int __get_data_block(struct inode *inode, sector_t iblock,
1760 struct buffer_head *bh, int create, int flag, 1773 struct buffer_head *bh, int create, int flag,
1761 pgoff_t *next_pgofs, int seg_type, bool may_write) 1774 pgoff_t *next_pgofs, int seg_type, bool may_write)
@@ -1764,7 +1777,7 @@ static int __get_data_block(struct inode *inode, sector_t iblock,
1764 int err; 1777 int err;
1765 1778
1766 map.m_lblk = iblock; 1779 map.m_lblk = iblock;
1767 map.m_len = bh->b_size >> inode->i_blkbits; 1780 map.m_len = bytes_to_blks(inode, bh->b_size);
1768 map.m_next_pgofs = next_pgofs; 1781 map.m_next_pgofs = next_pgofs;
1769 map.m_next_extent = NULL; 1782 map.m_next_extent = NULL;
1770 map.m_seg_type = seg_type; 1783 map.m_seg_type = seg_type;
@@ -1774,20 +1787,11 @@ static int __get_data_block(struct inode *inode, sector_t iblock,
1774 if (!err) { 1787 if (!err) {
1775 map_bh(bh, inode->i_sb, map.m_pblk); 1788 map_bh(bh, inode->i_sb, map.m_pblk);
1776 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags; 1789 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1777 bh->b_size = (u64)map.m_len << inode->i_blkbits; 1790 bh->b_size = blks_to_bytes(inode, map.m_len);
1778 } 1791 }
1779 return err; 1792 return err;
1780} 1793}
1781 1794
1782static int get_data_block(struct inode *inode, sector_t iblock,
1783 struct buffer_head *bh_result, int create, int flag,
1784 pgoff_t *next_pgofs)
1785{
1786 return __get_data_block(inode, iblock, bh_result, create,
1787 flag, next_pgofs,
1788 NO_CHECK_TYPE, create);
1789}
1790
1791static int get_data_block_dio_write(struct inode *inode, sector_t iblock, 1795static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1792 struct buffer_head *bh_result, int create) 1796 struct buffer_head *bh_result, int create)
1793{ 1797{
@@ -1806,24 +1810,6 @@ static int get_data_block_dio(struct inode *inode, sector_t iblock,
1806 false); 1810 false);
1807} 1811}
1808 1812
1809static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1810 struct buffer_head *bh_result, int create)
1811{
1812 return __get_data_block(inode, iblock, bh_result, create,
1813 F2FS_GET_BLOCK_BMAP, NULL,
1814 NO_CHECK_TYPE, create);
1815}
1816
1817static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1818{
1819 return (offset >> inode->i_blkbits);
1820}
1821
1822static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1823{
1824 return (blk << inode->i_blkbits);
1825}
1826
1827static int f2fs_xattr_fiemap(struct inode *inode, 1813static int f2fs_xattr_fiemap(struct inode *inode,
1828 struct fiemap_extent_info *fieinfo) 1814 struct fiemap_extent_info *fieinfo)
1829{ 1815{
@@ -1849,7 +1835,7 @@ static int f2fs_xattr_fiemap(struct inode *inode,
1849 return err; 1835 return err;
1850 } 1836 }
1851 1837
1852 phys = (__u64)blk_to_logical(inode, ni.blk_addr); 1838 phys = blks_to_bytes(inode, ni.blk_addr);
1853 offset = offsetof(struct f2fs_inode, i_addr) + 1839 offset = offsetof(struct f2fs_inode, i_addr) +
1854 sizeof(__le32) * (DEF_ADDRS_PER_INODE - 1840 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1855 get_inline_xattr_addrs(inode)); 1841 get_inline_xattr_addrs(inode));
@@ -1881,7 +1867,7 @@ static int f2fs_xattr_fiemap(struct inode *inode,
1881 return err; 1867 return err;
1882 } 1868 }
1883 1869
1884 phys = (__u64)blk_to_logical(inode, ni.blk_addr); 1870 phys = blks_to_bytes(inode, ni.blk_addr);
1885 len = inode->i_sb->s_blocksize; 1871 len = inode->i_sb->s_blocksize;
1886 1872
1887 f2fs_put_page(page, 1); 1873 f2fs_put_page(page, 1);
@@ -1919,7 +1905,7 @@ static loff_t max_inode_blocks(struct inode *inode)
1919int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 1905int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1920 u64 start, u64 len) 1906 u64 start, u64 len)
1921{ 1907{
1922 struct buffer_head map_bh; 1908 struct f2fs_map_blocks map;
1923 sector_t start_blk, last_blk; 1909 sector_t start_blk, last_blk;
1924 pgoff_t next_pgofs; 1910 pgoff_t next_pgofs;
1925 u64 logical = 0, phys = 0, size = 0; 1911 u64 logical = 0, phys = 0, size = 0;
@@ -1951,29 +1937,31 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1951 goto out; 1937 goto out;
1952 } 1938 }
1953 1939
1954 if (logical_to_blk(inode, len) == 0) 1940 if (bytes_to_blks(inode, len) == 0)
1955 len = blk_to_logical(inode, 1); 1941 len = blks_to_bytes(inode, 1);
1956 1942
1957 start_blk = logical_to_blk(inode, start); 1943 start_blk = bytes_to_blks(inode, start);
1958 last_blk = logical_to_blk(inode, start + len - 1); 1944 last_blk = bytes_to_blks(inode, start + len - 1);
1959 1945
1960next: 1946next:
1961 memset(&map_bh, 0, sizeof(struct buffer_head)); 1947 memset(&map, 0, sizeof(map));
1962 map_bh.b_size = len; 1948 map.m_lblk = start_blk;
1949 map.m_len = bytes_to_blks(inode, len);
1950 map.m_next_pgofs = &next_pgofs;
1951 map.m_seg_type = NO_CHECK_TYPE;
1963 1952
1964 if (compr_cluster) 1953 if (compr_cluster)
1965 map_bh.b_size = blk_to_logical(inode, cluster_size - 1); 1954 map.m_len = cluster_size - 1;
1966 1955
1967 ret = get_data_block(inode, start_blk, &map_bh, 0, 1956 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
1968 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1969 if (ret) 1957 if (ret)
1970 goto out; 1958 goto out;
1971 1959
1972 /* HOLE */ 1960 /* HOLE */
1973 if (!buffer_mapped(&map_bh)) { 1961 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1974 start_blk = next_pgofs; 1962 start_blk = next_pgofs;
1975 1963
1976 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode, 1964 if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
1977 max_inode_blocks(inode))) 1965 max_inode_blocks(inode)))
1978 goto prep_next; 1966 goto prep_next;
1979 1967
@@ -1999,9 +1987,9 @@ next:
1999 compr_cluster = false; 1987 compr_cluster = false;
2000 1988
2001 1989
2002 logical = blk_to_logical(inode, start_blk - 1); 1990 logical = blks_to_bytes(inode, start_blk - 1);
2003 phys = blk_to_logical(inode, map_bh.b_blocknr); 1991 phys = blks_to_bytes(inode, map.m_pblk);
2004 size = blk_to_logical(inode, cluster_size); 1992 size = blks_to_bytes(inode, cluster_size);
2005 1993
2006 flags |= FIEMAP_EXTENT_ENCODED; 1994 flags |= FIEMAP_EXTENT_ENCODED;
2007 1995
@@ -2013,20 +2001,20 @@ next:
2013 goto prep_next; 2001 goto prep_next;
2014 } 2002 }
2015 2003
2016 if (map_bh.b_blocknr == COMPRESS_ADDR) { 2004 if (map.m_pblk == COMPRESS_ADDR) {
2017 compr_cluster = true; 2005 compr_cluster = true;
2018 start_blk++; 2006 start_blk++;
2019 goto prep_next; 2007 goto prep_next;
2020 } 2008 }
2021 2009
2022 logical = blk_to_logical(inode, start_blk); 2010 logical = blks_to_bytes(inode, start_blk);
2023 phys = blk_to_logical(inode, map_bh.b_blocknr); 2011 phys = blks_to_bytes(inode, map.m_pblk);
2024 size = map_bh.b_size; 2012 size = blks_to_bytes(inode, map.m_len);
2025 flags = 0; 2013 flags = 0;
2026 if (buffer_unwritten(&map_bh)) 2014 if (map.m_flags & F2FS_MAP_UNWRITTEN)
2027 flags = FIEMAP_EXTENT_UNWRITTEN; 2015 flags = FIEMAP_EXTENT_UNWRITTEN;
2028 2016
2029 start_blk += logical_to_blk(inode, size); 2017 start_blk += bytes_to_blks(inode, size);
2030 2018
2031prep_next: 2019prep_next:
2032 cond_resched(); 2020 cond_resched();
@@ -2059,8 +2047,7 @@ static int f2fs_read_single_page(struct inode *inode, struct page *page,
2059 bool is_readahead) 2047 bool is_readahead)
2060{ 2048{
2061 struct bio *bio = *bio_ret; 2049 struct bio *bio = *bio_ret;
2062 const unsigned blkbits = inode->i_blkbits; 2050 const unsigned blocksize = blks_to_bytes(inode, 1);
2063 const unsigned blocksize = 1 << blkbits;
2064 sector_t block_in_file; 2051 sector_t block_in_file;
2065 sector_t last_block; 2052 sector_t last_block;
2066 sector_t last_block_in_file; 2053 sector_t last_block_in_file;
@@ -2069,8 +2056,8 @@ static int f2fs_read_single_page(struct inode *inode, struct page *page,
2069 2056
2070 block_in_file = (sector_t)page_index(page); 2057 block_in_file = (sector_t)page_index(page);
2071 last_block = block_in_file + nr_pages; 2058 last_block = block_in_file + nr_pages;
2072 last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >> 2059 last_block_in_file = bytes_to_blks(inode,
2073 blkbits; 2060 f2fs_readpage_limit(inode) + blocksize - 1);
2074 if (last_block > last_block_in_file) 2061 if (last_block > last_block_in_file)
2075 last_block = last_block_in_file; 2062 last_block = last_block_in_file;
2076 2063
@@ -2183,8 +2170,7 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2183 struct bio *bio = *bio_ret; 2170 struct bio *bio = *bio_ret;
2184 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size; 2171 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2185 sector_t last_block_in_file; 2172 sector_t last_block_in_file;
2186 const unsigned blkbits = inode->i_blkbits; 2173 const unsigned blocksize = blks_to_bytes(inode, 1);
2187 const unsigned blocksize = 1 << blkbits;
2188 struct decompress_io_ctx *dic = NULL; 2174 struct decompress_io_ctx *dic = NULL;
2189 struct bio_post_read_ctx *ctx; 2175 struct bio_post_read_ctx *ctx;
2190 bool for_verity = false; 2176 bool for_verity = false;
@@ -2193,8 +2179,8 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2193 2179
2194 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc)); 2180 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2195 2181
2196 last_block_in_file = (f2fs_readpage_limit(inode) + 2182 last_block_in_file = bytes_to_blks(inode,
2197 blocksize - 1) >> blkbits; 2183 f2fs_readpage_limit(inode) + blocksize - 1);
2198 2184
2199 /* get rid of pages beyond EOF */ 2185 /* get rid of pages beyond EOF */
2200 for (i = 0; i < cc->cluster_size; i++) { 2186 for (i = 0; i < cc->cluster_size; i++) {
@@ -3205,7 +3191,7 @@ static inline bool __should_serialize_io(struct inode *inode,
3205 if (IS_NOQUOTA(inode)) 3191 if (IS_NOQUOTA(inode))
3206 return false; 3192 return false;
3207 3193
3208 if (f2fs_compressed_file(inode)) 3194 if (f2fs_need_compress_data(inode))
3209 return true; 3195 return true;
3210 if (wbc->sync_mode != WB_SYNC_ALL) 3196 if (wbc->sync_mode != WB_SYNC_ALL)
3211 return true; 3197 return true;
@@ -3881,9 +3867,6 @@ static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3881static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) 3867static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3882{ 3868{
3883 struct inode *inode = mapping->host; 3869 struct inode *inode = mapping->host;
3884 struct buffer_head tmp = {
3885 .b_size = i_blocksize(inode),
3886 };
3887 sector_t blknr = 0; 3870 sector_t blknr = 0;
3888 3871
3889 if (f2fs_has_inline_data(inode)) 3872 if (f2fs_has_inline_data(inode))
@@ -3900,8 +3883,16 @@ static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3900 if (f2fs_compressed_file(inode)) { 3883 if (f2fs_compressed_file(inode)) {
3901 blknr = f2fs_bmap_compress(inode, block); 3884 blknr = f2fs_bmap_compress(inode, block);
3902 } else { 3885 } else {
3903 if (!get_data_block_bmap(inode, block, &tmp, 0)) 3886 struct f2fs_map_blocks map;
3904 blknr = tmp.b_blocknr; 3887
3888 memset(&map, 0, sizeof(map));
3889 map.m_lblk = block;
3890 map.m_len = 1;
3891 map.m_next_pgofs = NULL;
3892 map.m_seg_type = NO_CHECK_TYPE;
3893
3894 if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3895 blknr = map.m_pblk;
3905 } 3896 }
3906out: 3897out:
3907 trace_f2fs_bmap(inode, block, blknr); 3898 trace_f2fs_bmap(inode, block, blknr);
@@ -3977,7 +3968,7 @@ static int check_swap_activate_fast(struct swap_info_struct *sis,
3977 sector_t highest_pblock = 0; 3968 sector_t highest_pblock = 0;
3978 int nr_extents = 0; 3969 int nr_extents = 0;
3979 unsigned long nr_pblocks; 3970 unsigned long nr_pblocks;
3980 unsigned long len; 3971 u64 len;
3981 int ret; 3972 int ret;
3982 3973
3983 /* 3974 /*
@@ -3985,29 +3976,31 @@ static int check_swap_activate_fast(struct swap_info_struct *sis,
3985 * to be very smart. 3976 * to be very smart.
3986 */ 3977 */
3987 cur_lblock = 0; 3978 cur_lblock = 0;
3988 last_lblock = logical_to_blk(inode, i_size_read(inode)); 3979 last_lblock = bytes_to_blks(inode, i_size_read(inode));
3989 len = i_size_read(inode); 3980 len = i_size_read(inode);
3990 3981
3991 while (cur_lblock <= last_lblock && cur_lblock < sis->max) { 3982 while (cur_lblock <= last_lblock && cur_lblock < sis->max) {
3992 struct buffer_head map_bh; 3983 struct f2fs_map_blocks map;
3993 pgoff_t next_pgofs; 3984 pgoff_t next_pgofs;
3994 3985
3995 cond_resched(); 3986 cond_resched();
3996 3987
3997 memset(&map_bh, 0, sizeof(struct buffer_head)); 3988 memset(&map, 0, sizeof(map));
3998 map_bh.b_size = len - cur_lblock; 3989 map.m_lblk = cur_lblock;
3990 map.m_len = bytes_to_blks(inode, len) - cur_lblock;
3991 map.m_next_pgofs = &next_pgofs;
3992 map.m_seg_type = NO_CHECK_TYPE;
3999 3993
4000 ret = get_data_block(inode, cur_lblock, &map_bh, 0, 3994 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
4001 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
4002 if (ret) 3995 if (ret)
4003 goto err_out; 3996 goto err_out;
4004 3997
4005 /* hole */ 3998 /* hole */
4006 if (!buffer_mapped(&map_bh)) 3999 if (!(map.m_flags & F2FS_MAP_FLAGS))
4007 goto err_out; 4000 goto err_out;
4008 4001
4009 pblock = map_bh.b_blocknr; 4002 pblock = map.m_pblk;
4010 nr_pblocks = logical_to_blk(inode, map_bh.b_size); 4003 nr_pblocks = map.m_len;
4011 4004
4012 if (cur_lblock + nr_pblocks >= sis->max) 4005 if (cur_lblock + nr_pblocks >= sis->max)
4013 nr_pblocks = sis->max - cur_lblock; 4006 nr_pblocks = sis->max - cur_lblock;
@@ -4050,7 +4043,6 @@ static int check_swap_activate(struct swap_info_struct *sis,
4050 struct inode *inode = mapping->host; 4043 struct inode *inode = mapping->host;
4051 unsigned blocks_per_page; 4044 unsigned blocks_per_page;
4052 unsigned long page_no; 4045 unsigned long page_no;
4053 unsigned blkbits;
4054 sector_t probe_block; 4046 sector_t probe_block;
4055 sector_t last_block; 4047 sector_t last_block;
4056 sector_t lowest_block = -1; 4048 sector_t lowest_block = -1;
@@ -4061,8 +4053,7 @@ static int check_swap_activate(struct swap_info_struct *sis,
4061 if (PAGE_SIZE == F2FS_BLKSIZE) 4053 if (PAGE_SIZE == F2FS_BLKSIZE)
4062 return check_swap_activate_fast(sis, swap_file, span); 4054 return check_swap_activate_fast(sis, swap_file, span);
4063 4055
4064 blkbits = inode->i_blkbits; 4056 blocks_per_page = bytes_to_blks(inode, PAGE_SIZE);
4065 blocks_per_page = PAGE_SIZE >> blkbits;
4066 4057
4067 /* 4058 /*
4068 * Map all the blocks into the extent list. This code doesn't try 4059 * Map all the blocks into the extent list. This code doesn't try
@@ -4070,7 +4061,7 @@ static int check_swap_activate(struct swap_info_struct *sis,
4070 */ 4061 */
4071 probe_block = 0; 4062 probe_block = 0;
4072 page_no = 0; 4063 page_no = 0;
4073 last_block = i_size_read(inode) >> blkbits; 4064 last_block = bytes_to_blks(inode, i_size_read(inode));
4074 while ((probe_block + blocks_per_page) <= last_block && 4065 while ((probe_block + blocks_per_page) <= last_block &&
4075 page_no < sis->max) { 4066 page_no < sis->max) {
4076 unsigned block_in_page; 4067 unsigned block_in_page;
@@ -4110,7 +4101,7 @@ static int check_swap_activate(struct swap_info_struct *sis,
4110 } 4101 }
4111 } 4102 }
4112 4103
4113 first_block >>= (PAGE_SHIFT - blkbits); 4104 first_block >>= (PAGE_SHIFT - inode->i_blkbits);
4114 if (page_no) { /* exclude the header page */ 4105 if (page_no) { /* exclude the header page */
4115 if (first_block < lowest_block) 4106 if (first_block < lowest_block)
4116 lowest_block = first_block; 4107 lowest_block = first_block;
diff --git a/fs/f2fs/debug.c b/fs/f2fs/debug.c
index a8357fd4f5fa..197c914119da 100644
--- a/fs/f2fs/debug.c
+++ b/fs/f2fs/debug.c
@@ -145,8 +145,8 @@ static void update_general_status(struct f2fs_sb_info *sbi)
145 si->node_pages = NODE_MAPPING(sbi)->nrpages; 145 si->node_pages = NODE_MAPPING(sbi)->nrpages;
146 if (sbi->meta_inode) 146 if (sbi->meta_inode)
147 si->meta_pages = META_MAPPING(sbi)->nrpages; 147 si->meta_pages = META_MAPPING(sbi)->nrpages;
148 si->nats = NM_I(sbi)->nat_cnt; 148 si->nats = NM_I(sbi)->nat_cnt[TOTAL_NAT];
149 si->dirty_nats = NM_I(sbi)->dirty_nat_cnt; 149 si->dirty_nats = NM_I(sbi)->nat_cnt[DIRTY_NAT];
150 si->sits = MAIN_SEGS(sbi); 150 si->sits = MAIN_SEGS(sbi);
151 si->dirty_sits = SIT_I(sbi)->dirty_sentries; 151 si->dirty_sits = SIT_I(sbi)->dirty_sentries;
152 si->free_nids = NM_I(sbi)->nid_cnt[FREE_NID]; 152 si->free_nids = NM_I(sbi)->nid_cnt[FREE_NID];
@@ -278,9 +278,10 @@ get_cache:
278 si->cache_mem += (NM_I(sbi)->nid_cnt[FREE_NID] + 278 si->cache_mem += (NM_I(sbi)->nid_cnt[FREE_NID] +
279 NM_I(sbi)->nid_cnt[PREALLOC_NID]) * 279 NM_I(sbi)->nid_cnt[PREALLOC_NID]) *
280 sizeof(struct free_nid); 280 sizeof(struct free_nid);
281 si->cache_mem += NM_I(sbi)->nat_cnt * sizeof(struct nat_entry); 281 si->cache_mem += NM_I(sbi)->nat_cnt[TOTAL_NAT] *
282 si->cache_mem += NM_I(sbi)->dirty_nat_cnt * 282 sizeof(struct nat_entry);
283 sizeof(struct nat_entry_set); 283 si->cache_mem += NM_I(sbi)->nat_cnt[DIRTY_NAT] *
284 sizeof(struct nat_entry_set);
284 si->cache_mem += si->inmem_pages * sizeof(struct inmem_pages); 285 si->cache_mem += si->inmem_pages * sizeof(struct inmem_pages);
285 for (i = 0; i < MAX_INO_ENTRY; i++) 286 for (i = 0; i < MAX_INO_ENTRY; i++)
286 si->cache_mem += sbi->im[i].ino_num * sizeof(struct ino_entry); 287 si->cache_mem += sbi->im[i].ino_num * sizeof(struct ino_entry);
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 2675490158a9..5130423a13e7 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -33,10 +33,8 @@
33#else 33#else
34#define f2fs_bug_on(sbi, condition) \ 34#define f2fs_bug_on(sbi, condition) \
35 do { \ 35 do { \
36 if (unlikely(condition)) { \ 36 if (WARN_ON(condition)) \
37 WARN_ON(1); \
38 set_sbi_flag(sbi, SBI_NEED_FSCK); \ 37 set_sbi_flag(sbi, SBI_NEED_FSCK); \
39 } \
40 } while (0) 38 } while (0)
41#endif 39#endif
42 40
@@ -147,8 +145,10 @@ struct f2fs_mount_info {
147 145
148 /* For compression */ 146 /* For compression */
149 unsigned char compress_algorithm; /* algorithm type */ 147 unsigned char compress_algorithm; /* algorithm type */
150 unsigned compress_log_size; /* cluster log size */ 148 unsigned char compress_log_size; /* cluster log size */
149 bool compress_chksum; /* compressed data chksum */
151 unsigned char compress_ext_cnt; /* extension count */ 150 unsigned char compress_ext_cnt; /* extension count */
151 int compress_mode; /* compression mode */
152 unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */ 152 unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
153}; 153};
154 154
@@ -402,85 +402,6 @@ static inline bool __has_cursum_space(struct f2fs_journal *journal,
402 return size <= MAX_SIT_JENTRIES(journal); 402 return size <= MAX_SIT_JENTRIES(journal);
403} 403}
404 404
405/*
406 * f2fs-specific ioctl commands
407 */
408#define F2FS_IOCTL_MAGIC 0xf5
409#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
410#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
411#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
412#define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
413#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
414#define F2FS_IOC_GARBAGE_COLLECT _IOW(F2FS_IOCTL_MAGIC, 6, __u32)
415#define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
416#define F2FS_IOC_DEFRAGMENT _IOWR(F2FS_IOCTL_MAGIC, 8, \
417 struct f2fs_defragment)
418#define F2FS_IOC_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
419 struct f2fs_move_range)
420#define F2FS_IOC_FLUSH_DEVICE _IOW(F2FS_IOCTL_MAGIC, 10, \
421 struct f2fs_flush_device)
422#define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \
423 struct f2fs_gc_range)
424#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, __u32)
425#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
426#define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
427#define F2FS_IOC_PRECACHE_EXTENTS _IO(F2FS_IOCTL_MAGIC, 15)
428#define F2FS_IOC_RESIZE_FS _IOW(F2FS_IOCTL_MAGIC, 16, __u64)
429#define F2FS_IOC_GET_COMPRESS_BLOCKS _IOR(F2FS_IOCTL_MAGIC, 17, __u64)
430#define F2FS_IOC_RELEASE_COMPRESS_BLOCKS \
431 _IOR(F2FS_IOCTL_MAGIC, 18, __u64)
432#define F2FS_IOC_RESERVE_COMPRESS_BLOCKS \
433 _IOR(F2FS_IOCTL_MAGIC, 19, __u64)
434#define F2FS_IOC_SEC_TRIM_FILE _IOW(F2FS_IOCTL_MAGIC, 20, \
435 struct f2fs_sectrim_range)
436
437/*
438 * should be same as XFS_IOC_GOINGDOWN.
439 * Flags for going down operation used by FS_IOC_GOINGDOWN
440 */
441#define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
442#define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
443#define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
444#define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
445#define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
446#define F2FS_GOING_DOWN_NEED_FSCK 0x4 /* going down to trigger fsck */
447
448/*
449 * Flags used by F2FS_IOC_SEC_TRIM_FILE
450 */
451#define F2FS_TRIM_FILE_DISCARD 0x1 /* send discard command */
452#define F2FS_TRIM_FILE_ZEROOUT 0x2 /* zero out */
453#define F2FS_TRIM_FILE_MASK 0x3
454
455struct f2fs_gc_range {
456 u32 sync;
457 u64 start;
458 u64 len;
459};
460
461struct f2fs_defragment {
462 u64 start;
463 u64 len;
464};
465
466struct f2fs_move_range {
467 u32 dst_fd; /* destination fd */
468 u64 pos_in; /* start position in src_fd */
469 u64 pos_out; /* start position in dst_fd */
470 u64 len; /* size to move */
471};
472
473struct f2fs_flush_device {
474 u32 dev_num; /* device number to flush */
475 u32 segments; /* # of segments to flush */
476};
477
478struct f2fs_sectrim_range {
479 u64 start;
480 u64 len;
481 u64 flags;
482};
483
484/* for inline stuff */ 405/* for inline stuff */
485#define DEF_INLINE_RESERVED_SIZE 1 406#define DEF_INLINE_RESERVED_SIZE 1
486static inline int get_extra_isize(struct inode *inode); 407static inline int get_extra_isize(struct inode *inode);
@@ -755,7 +676,9 @@ enum {
755 FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */ 676 FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
756 FI_VERITY_IN_PROGRESS, /* building fs-verity Merkle tree */ 677 FI_VERITY_IN_PROGRESS, /* building fs-verity Merkle tree */
757 FI_COMPRESSED_FILE, /* indicate file's data can be compressed */ 678 FI_COMPRESSED_FILE, /* indicate file's data can be compressed */
679 FI_COMPRESS_CORRUPT, /* indicate compressed cluster is corrupted */
758 FI_MMAP_FILE, /* indicate file was mmapped */ 680 FI_MMAP_FILE, /* indicate file was mmapped */
681 FI_ENABLE_COMPRESS, /* enable compression in "user" compression mode */
759 FI_MAX, /* max flag, never be used */ 682 FI_MAX, /* max flag, never be used */
760}; 683};
761 684
@@ -812,6 +735,7 @@ struct f2fs_inode_info {
812 atomic_t i_compr_blocks; /* # of compressed blocks */ 735 atomic_t i_compr_blocks; /* # of compressed blocks */
813 unsigned char i_compress_algorithm; /* algorithm type */ 736 unsigned char i_compress_algorithm; /* algorithm type */
814 unsigned char i_log_cluster_size; /* log of cluster size */ 737 unsigned char i_log_cluster_size; /* log of cluster size */
738 unsigned short i_compress_flag; /* compress flag */
815 unsigned int i_cluster_size; /* cluster size */ 739 unsigned int i_cluster_size; /* cluster size */
816}; 740};
817 741
@@ -896,6 +820,13 @@ enum nid_state {
896 MAX_NID_STATE, 820 MAX_NID_STATE,
897}; 821};
898 822
823enum nat_state {
824 TOTAL_NAT,
825 DIRTY_NAT,
826 RECLAIMABLE_NAT,
827 MAX_NAT_STATE,
828};
829
899struct f2fs_nm_info { 830struct f2fs_nm_info {
900 block_t nat_blkaddr; /* base disk address of NAT */ 831 block_t nat_blkaddr; /* base disk address of NAT */
901 nid_t max_nid; /* maximum possible node ids */ 832 nid_t max_nid; /* maximum possible node ids */
@@ -911,8 +842,7 @@ struct f2fs_nm_info {
911 struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */ 842 struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
912 struct list_head nat_entries; /* cached nat entry list (clean) */ 843 struct list_head nat_entries; /* cached nat entry list (clean) */
913 spinlock_t nat_list_lock; /* protect clean nat entry list */ 844 spinlock_t nat_list_lock; /* protect clean nat entry list */
914 unsigned int nat_cnt; /* the # of cached nat entries */ 845 unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
915 unsigned int dirty_nat_cnt; /* total num of nat entries in set */
916 unsigned int nat_blocks; /* # of nat blocks */ 846 unsigned int nat_blocks; /* # of nat blocks */
917 847
918 /* free node ids management */ 848 /* free node ids management */
@@ -1322,6 +1252,18 @@ enum fsync_mode {
1322 FSYNC_MODE_NOBARRIER, /* fsync behaves nobarrier based on posix */ 1252 FSYNC_MODE_NOBARRIER, /* fsync behaves nobarrier based on posix */
1323}; 1253};
1324 1254
1255enum {
1256 COMPR_MODE_FS, /*
1257 * automatically compress compression
1258 * enabled files
1259 */
1260 COMPR_MODE_USER, /*
1261 * automatical compression is disabled.
1262 * user can control the file compression
1263 * using ioctls
1264 */
1265};
1266
1325/* 1267/*
1326 * this value is set in page as a private data which indicate that 1268 * this value is set in page as a private data which indicate that
1327 * the page is atomically written, and it is in inmem_pages list. 1269 * the page is atomically written, and it is in inmem_pages list.
@@ -1351,9 +1293,15 @@ enum compress_algorithm_type {
1351 COMPRESS_MAX, 1293 COMPRESS_MAX,
1352}; 1294};
1353 1295
1354#define COMPRESS_DATA_RESERVED_SIZE 5 1296enum compress_flag {
1297 COMPRESS_CHKSUM,
1298 COMPRESS_MAX_FLAG,
1299};
1300
1301#define COMPRESS_DATA_RESERVED_SIZE 4
1355struct compress_data { 1302struct compress_data {
1356 __le32 clen; /* compressed data size */ 1303 __le32 clen; /* compressed data size */
1304 __le32 chksum; /* compressed data chksum */
1357 __le32 reserved[COMPRESS_DATA_RESERVED_SIZE]; /* reserved */ 1305 __le32 reserved[COMPRESS_DATA_RESERVED_SIZE]; /* reserved */
1358 u8 cdata[]; /* compressed data */ 1306 u8 cdata[]; /* compressed data */
1359}; 1307};
@@ -1449,7 +1397,7 @@ struct f2fs_sb_info {
1449 int cur_cp_pack; /* remain current cp pack */ 1397 int cur_cp_pack; /* remain current cp pack */
1450 spinlock_t cp_lock; /* for flag in ckpt */ 1398 spinlock_t cp_lock; /* for flag in ckpt */
1451 struct inode *meta_inode; /* cache meta blocks */ 1399 struct inode *meta_inode; /* cache meta blocks */
1452 struct mutex cp_mutex; /* checkpoint procedure lock */ 1400 struct rw_semaphore cp_global_sem; /* checkpoint procedure lock */
1453 struct rw_semaphore cp_rwsem; /* blocking FS operations */ 1401 struct rw_semaphore cp_rwsem; /* blocking FS operations */
1454 struct rw_semaphore node_write; /* locking node writes */ 1402 struct rw_semaphore node_write; /* locking node writes */
1455 struct rw_semaphore node_change; /* locking node change */ 1403 struct rw_semaphore node_change; /* locking node change */
@@ -1499,6 +1447,7 @@ struct f2fs_sb_info {
1499 loff_t max_file_blocks; /* max block index of file */ 1447 loff_t max_file_blocks; /* max block index of file */
1500 int dir_level; /* directory level */ 1448 int dir_level; /* directory level */
1501 int readdir_ra; /* readahead inode in readdir */ 1449 int readdir_ra; /* readahead inode in readdir */
1450 u64 max_io_bytes; /* max io bytes to merge IOs */
1502 1451
1503 block_t user_block_count; /* # of user blocks */ 1452 block_t user_block_count; /* # of user blocks */
1504 block_t total_valid_block_count; /* # of valid blocks */ 1453 block_t total_valid_block_count; /* # of valid blocks */
@@ -1674,13 +1623,6 @@ static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
1674 return sbi->s_ndevs > 1; 1623 return sbi->s_ndevs > 1;
1675} 1624}
1676 1625
1677/* For write statistics. Suppose sector size is 512 bytes,
1678 * and the return value is in kbytes. s is of struct f2fs_sb_info.
1679 */
1680#define BD_PART_WRITTEN(s) \
1681(((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[STAT_WRITE]) - \
1682 (s)->sectors_written_start) >> 1)
1683
1684static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type) 1626static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
1685{ 1627{
1686 unsigned long now = jiffies; 1628 unsigned long now = jiffies;
@@ -2480,24 +2422,31 @@ static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
2480 return entry; 2422 return entry;
2481} 2423}
2482 2424
2483static inline bool is_idle(struct f2fs_sb_info *sbi, int type) 2425static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
2484{ 2426{
2485 if (sbi->gc_mode == GC_URGENT_HIGH)
2486 return true;
2487
2488 if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) || 2427 if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
2489 get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) || 2428 get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2490 get_pages(sbi, F2FS_WB_CP_DATA) || 2429 get_pages(sbi, F2FS_WB_CP_DATA) ||
2491 get_pages(sbi, F2FS_DIO_READ) || 2430 get_pages(sbi, F2FS_DIO_READ) ||
2492 get_pages(sbi, F2FS_DIO_WRITE)) 2431 get_pages(sbi, F2FS_DIO_WRITE))
2493 return false; 2432 return true;
2494 2433
2495 if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info && 2434 if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
2496 atomic_read(&SM_I(sbi)->dcc_info->queued_discard)) 2435 atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
2497 return false; 2436 return true;
2498 2437
2499 if (SM_I(sbi) && SM_I(sbi)->fcc_info && 2438 if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
2500 atomic_read(&SM_I(sbi)->fcc_info->queued_flush)) 2439 atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
2440 return true;
2441 return false;
2442}
2443
2444static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
2445{
2446 if (sbi->gc_mode == GC_URGENT_HIGH)
2447 return true;
2448
2449 if (is_inflight_io(sbi, type))
2501 return false; 2450 return false;
2502 2451
2503 if (sbi->gc_mode == GC_URGENT_LOW && 2452 if (sbi->gc_mode == GC_URGENT_LOW &&
@@ -2832,6 +2781,22 @@ static inline int f2fs_compressed_file(struct inode *inode)
2832 is_inode_flag_set(inode, FI_COMPRESSED_FILE); 2781 is_inode_flag_set(inode, FI_COMPRESSED_FILE);
2833} 2782}
2834 2783
2784static inline bool f2fs_need_compress_data(struct inode *inode)
2785{
2786 int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
2787
2788 if (!f2fs_compressed_file(inode))
2789 return false;
2790
2791 if (compress_mode == COMPR_MODE_FS)
2792 return true;
2793 else if (compress_mode == COMPR_MODE_USER &&
2794 is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
2795 return true;
2796
2797 return false;
2798}
2799
2835static inline unsigned int addrs_per_inode(struct inode *inode) 2800static inline unsigned int addrs_per_inode(struct inode *inode)
2836{ 2801{
2837 unsigned int addrs = CUR_ADDRS_PER_INODE(inode) - 2802 unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
@@ -3448,6 +3413,7 @@ void f2fs_update_dirty_page(struct inode *inode, struct page *page);
3448void f2fs_remove_dirty_inode(struct inode *inode); 3413void f2fs_remove_dirty_inode(struct inode *inode);
3449int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type); 3414int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
3450void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type); 3415void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
3416u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
3451int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc); 3417int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3452void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi); 3418void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
3453int __init f2fs_create_checkpoint_caches(void); 3419int __init f2fs_create_checkpoint_caches(void);
@@ -3963,6 +3929,9 @@ static inline void set_compress_context(struct inode *inode)
3963 F2FS_OPTION(sbi).compress_algorithm; 3929 F2FS_OPTION(sbi).compress_algorithm;
3964 F2FS_I(inode)->i_log_cluster_size = 3930 F2FS_I(inode)->i_log_cluster_size =
3965 F2FS_OPTION(sbi).compress_log_size; 3931 F2FS_OPTION(sbi).compress_log_size;
3932 F2FS_I(inode)->i_compress_flag =
3933 F2FS_OPTION(sbi).compress_chksum ?
3934 1 << COMPRESS_CHKSUM : 0;
3966 F2FS_I(inode)->i_cluster_size = 3935 F2FS_I(inode)->i_cluster_size =
3967 1 << F2FS_I(inode)->i_log_cluster_size; 3936 1 << F2FS_I(inode)->i_log_cluster_size;
3968 F2FS_I(inode)->i_flags |= F2FS_COMPR_FL; 3937 F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index fe39e591e5b4..16ea10f2bcf5 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -31,6 +31,7 @@
31#include "gc.h" 31#include "gc.h"
32#include "trace.h" 32#include "trace.h"
33#include <trace/events/f2fs.h> 33#include <trace/events/f2fs.h>
34#include <uapi/linux/f2fs.h>
34 35
35static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf) 36static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
36{ 37{
@@ -2479,26 +2480,19 @@ out:
2479 return ret; 2480 return ret;
2480} 2481}
2481 2482
2482static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg) 2483static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2483{ 2484{
2484 struct inode *inode = file_inode(filp); 2485 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2485 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2486 struct f2fs_gc_range range;
2487 u64 end; 2486 u64 end;
2488 int ret; 2487 int ret;
2489 2488
2490 if (!capable(CAP_SYS_ADMIN)) 2489 if (!capable(CAP_SYS_ADMIN))
2491 return -EPERM; 2490 return -EPERM;
2492
2493 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2494 sizeof(range)))
2495 return -EFAULT;
2496
2497 if (f2fs_readonly(sbi->sb)) 2491 if (f2fs_readonly(sbi->sb))
2498 return -EROFS; 2492 return -EROFS;
2499 2493
2500 end = range.start + range.len; 2494 end = range->start + range->len;
2501 if (end < range.start || range.start < MAIN_BLKADDR(sbi) || 2495 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2502 end >= MAX_BLKADDR(sbi)) 2496 end >= MAX_BLKADDR(sbi))
2503 return -EINVAL; 2497 return -EINVAL;
2504 2498
@@ -2507,7 +2501,7 @@ static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2507 return ret; 2501 return ret;
2508 2502
2509do_more: 2503do_more:
2510 if (!range.sync) { 2504 if (!range->sync) {
2511 if (!down_write_trylock(&sbi->gc_lock)) { 2505 if (!down_write_trylock(&sbi->gc_lock)) {
2512 ret = -EBUSY; 2506 ret = -EBUSY;
2513 goto out; 2507 goto out;
@@ -2516,20 +2510,30 @@ do_more:
2516 down_write(&sbi->gc_lock); 2510 down_write(&sbi->gc_lock);
2517 } 2511 }
2518 2512
2519 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start)); 2513 ret = f2fs_gc(sbi, range->sync, true, GET_SEGNO(sbi, range->start));
2520 if (ret) { 2514 if (ret) {
2521 if (ret == -EBUSY) 2515 if (ret == -EBUSY)
2522 ret = -EAGAIN; 2516 ret = -EAGAIN;
2523 goto out; 2517 goto out;
2524 } 2518 }
2525 range.start += BLKS_PER_SEC(sbi); 2519 range->start += BLKS_PER_SEC(sbi);
2526 if (range.start <= end) 2520 if (range->start <= end)
2527 goto do_more; 2521 goto do_more;
2528out: 2522out:
2529 mnt_drop_write_file(filp); 2523 mnt_drop_write_file(filp);
2530 return ret; 2524 return ret;
2531} 2525}
2532 2526
2527static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2528{
2529 struct f2fs_gc_range range;
2530
2531 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2532 sizeof(range)))
2533 return -EFAULT;
2534 return __f2fs_ioc_gc_range(filp, &range);
2535}
2536
2533static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg) 2537static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2534{ 2538{
2535 struct inode *inode = file_inode(filp); 2539 struct inode *inode = file_inode(filp);
@@ -2866,9 +2870,9 @@ out:
2866 return ret; 2870 return ret;
2867} 2871}
2868 2872
2869static int f2fs_ioc_move_range(struct file *filp, unsigned long arg) 2873static int __f2fs_ioc_move_range(struct file *filp,
2874 struct f2fs_move_range *range)
2870{ 2875{
2871 struct f2fs_move_range range;
2872 struct fd dst; 2876 struct fd dst;
2873 int err; 2877 int err;
2874 2878
@@ -2876,11 +2880,7 @@ static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2876 !(filp->f_mode & FMODE_WRITE)) 2880 !(filp->f_mode & FMODE_WRITE))
2877 return -EBADF; 2881 return -EBADF;
2878 2882
2879 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg, 2883 dst = fdget(range->dst_fd);
2880 sizeof(range)))
2881 return -EFAULT;
2882
2883 dst = fdget(range.dst_fd);
2884 if (!dst.file) 2884 if (!dst.file)
2885 return -EBADF; 2885 return -EBADF;
2886 2886
@@ -2893,21 +2893,25 @@ static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2893 if (err) 2893 if (err)
2894 goto err_out; 2894 goto err_out;
2895 2895
2896 err = f2fs_move_file_range(filp, range.pos_in, dst.file, 2896 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
2897 range.pos_out, range.len); 2897 range->pos_out, range->len);
2898 2898
2899 mnt_drop_write_file(filp); 2899 mnt_drop_write_file(filp);
2900 if (err)
2901 goto err_out;
2902
2903 if (copy_to_user((struct f2fs_move_range __user *)arg,
2904 &range, sizeof(range)))
2905 err = -EFAULT;
2906err_out: 2900err_out:
2907 fdput(dst); 2901 fdput(dst);
2908 return err; 2902 return err;
2909} 2903}
2910 2904
2905static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2906{
2907 struct f2fs_move_range range;
2908
2909 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2910 sizeof(range)))
2911 return -EFAULT;
2912 return __f2fs_ioc_move_range(filp, &range);
2913}
2914
2911static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg) 2915static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2912{ 2916{
2913 struct inode *inode = file_inode(filp); 2917 struct inode *inode = file_inode(filp);
@@ -3944,13 +3948,265 @@ err:
3944 return ret; 3948 return ret;
3945} 3949}
3946 3950
3947long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 3951static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
3948{ 3952{
3949 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp))))) 3953 struct inode *inode = file_inode(filp);
3950 return -EIO; 3954 struct f2fs_comp_option option;
3951 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp)))) 3955
3952 return -ENOSPC; 3956 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3957 return -EOPNOTSUPP;
3958
3959 inode_lock_shared(inode);
3960
3961 if (!f2fs_compressed_file(inode)) {
3962 inode_unlock_shared(inode);
3963 return -ENODATA;
3964 }
3965
3966 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
3967 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
3968
3969 inode_unlock_shared(inode);
3970
3971 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
3972 sizeof(option)))
3973 return -EFAULT;
3974
3975 return 0;
3976}
3977
3978static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
3979{
3980 struct inode *inode = file_inode(filp);
3981 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3982 struct f2fs_comp_option option;
3983 int ret = 0;
3984
3985 if (!f2fs_sb_has_compression(sbi))
3986 return -EOPNOTSUPP;
3987
3988 if (!(filp->f_mode & FMODE_WRITE))
3989 return -EBADF;
3990
3991 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
3992 sizeof(option)))
3993 return -EFAULT;
3994
3995 if (!f2fs_compressed_file(inode) ||
3996 option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
3997 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
3998 option.algorithm >= COMPRESS_MAX)
3999 return -EINVAL;
4000
4001 file_start_write(filp);
4002 inode_lock(inode);
4003
4004 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
4005 ret = -EBUSY;
4006 goto out;
4007 }
4008
4009 if (inode->i_size != 0) {
4010 ret = -EFBIG;
4011 goto out;
4012 }
4013
4014 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
4015 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
4016 F2FS_I(inode)->i_cluster_size = 1 << option.log_cluster_size;
4017 f2fs_mark_inode_dirty_sync(inode, true);
4018
4019 if (!f2fs_is_compress_backend_ready(inode))
4020 f2fs_warn(sbi, "compression algorithm is successfully set, "
4021 "but current kernel doesn't support this algorithm.");
4022out:
4023 inode_unlock(inode);
4024 file_end_write(filp);
4025
4026 return ret;
4027}
4028
4029static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
4030{
4031 DEFINE_READAHEAD(ractl, NULL, inode->i_mapping, page_idx);
4032 struct address_space *mapping = inode->i_mapping;
4033 struct page *page;
4034 pgoff_t redirty_idx = page_idx;
4035 int i, page_len = 0, ret = 0;
4036
4037 page_cache_ra_unbounded(&ractl, len, 0);
4038
4039 for (i = 0; i < len; i++, page_idx++) {
4040 page = read_cache_page(mapping, page_idx, NULL, NULL);
4041 if (IS_ERR(page)) {
4042 ret = PTR_ERR(page);
4043 break;
4044 }
4045 page_len++;
4046 }
4047
4048 for (i = 0; i < page_len; i++, redirty_idx++) {
4049 page = find_lock_page(mapping, redirty_idx);
4050 if (!page)
4051 ret = -ENOENT;
4052 set_page_dirty(page);
4053 f2fs_put_page(page, 1);
4054 f2fs_put_page(page, 0);
4055 }
4056
4057 return ret;
4058}
4059
4060static int f2fs_ioc_decompress_file(struct file *filp, unsigned long arg)
4061{
4062 struct inode *inode = file_inode(filp);
4063 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4064 struct f2fs_inode_info *fi = F2FS_I(inode);
4065 pgoff_t page_idx = 0, last_idx;
4066 unsigned int blk_per_seg = sbi->blocks_per_seg;
4067 int cluster_size = F2FS_I(inode)->i_cluster_size;
4068 int count, ret;
4069
4070 if (!f2fs_sb_has_compression(sbi) ||
4071 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4072 return -EOPNOTSUPP;
4073
4074 if (!(filp->f_mode & FMODE_WRITE))
4075 return -EBADF;
4076
4077 if (!f2fs_compressed_file(inode))
4078 return -EINVAL;
4079
4080 f2fs_balance_fs(F2FS_I_SB(inode), true);
4081
4082 file_start_write(filp);
4083 inode_lock(inode);
3953 4084
4085 if (!f2fs_is_compress_backend_ready(inode)) {
4086 ret = -EOPNOTSUPP;
4087 goto out;
4088 }
4089
4090 if (f2fs_is_mmap_file(inode)) {
4091 ret = -EBUSY;
4092 goto out;
4093 }
4094
4095 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4096 if (ret)
4097 goto out;
4098
4099 if (!atomic_read(&fi->i_compr_blocks))
4100 goto out;
4101
4102 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4103
4104 count = last_idx - page_idx;
4105 while (count) {
4106 int len = min(cluster_size, count);
4107
4108 ret = redirty_blocks(inode, page_idx, len);
4109 if (ret < 0)
4110 break;
4111
4112 if (get_dirty_pages(inode) >= blk_per_seg)
4113 filemap_fdatawrite(inode->i_mapping);
4114
4115 count -= len;
4116 page_idx += len;
4117 }
4118
4119 if (!ret)
4120 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4121 LLONG_MAX);
4122
4123 if (ret)
4124 f2fs_warn(sbi, "%s: The file might be partially decompressed "
4125 "(errno=%d). Please delete the file.\n",
4126 __func__, ret);
4127out:
4128 inode_unlock(inode);
4129 file_end_write(filp);
4130
4131 return ret;
4132}
4133
4134static int f2fs_ioc_compress_file(struct file *filp, unsigned long arg)
4135{
4136 struct inode *inode = file_inode(filp);
4137 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4138 pgoff_t page_idx = 0, last_idx;
4139 unsigned int blk_per_seg = sbi->blocks_per_seg;
4140 int cluster_size = F2FS_I(inode)->i_cluster_size;
4141 int count, ret;
4142
4143 if (!f2fs_sb_has_compression(sbi) ||
4144 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4145 return -EOPNOTSUPP;
4146
4147 if (!(filp->f_mode & FMODE_WRITE))
4148 return -EBADF;
4149
4150 if (!f2fs_compressed_file(inode))
4151 return -EINVAL;
4152
4153 f2fs_balance_fs(F2FS_I_SB(inode), true);
4154
4155 file_start_write(filp);
4156 inode_lock(inode);
4157
4158 if (!f2fs_is_compress_backend_ready(inode)) {
4159 ret = -EOPNOTSUPP;
4160 goto out;
4161 }
4162
4163 if (f2fs_is_mmap_file(inode)) {
4164 ret = -EBUSY;
4165 goto out;
4166 }
4167
4168 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4169 if (ret)
4170 goto out;
4171
4172 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4173
4174 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4175
4176 count = last_idx - page_idx;
4177 while (count) {
4178 int len = min(cluster_size, count);
4179
4180 ret = redirty_blocks(inode, page_idx, len);
4181 if (ret < 0)
4182 break;
4183
4184 if (get_dirty_pages(inode) >= blk_per_seg)
4185 filemap_fdatawrite(inode->i_mapping);
4186
4187 count -= len;
4188 page_idx += len;
4189 }
4190
4191 if (!ret)
4192 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4193 LLONG_MAX);
4194
4195 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4196
4197 if (ret)
4198 f2fs_warn(sbi, "%s: The file might be partially compressed "
4199 "(errno=%d). Please delete the file.\n",
4200 __func__, ret);
4201out:
4202 inode_unlock(inode);
4203 file_end_write(filp);
4204
4205 return ret;
4206}
4207
4208static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4209{
3954 switch (cmd) { 4210 switch (cmd) {
3955 case FS_IOC_GETFLAGS: 4211 case FS_IOC_GETFLAGS:
3956 return f2fs_ioc_getflags(filp, arg); 4212 return f2fs_ioc_getflags(filp, arg);
@@ -4032,11 +4288,29 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4032 return f2fs_reserve_compress_blocks(filp, arg); 4288 return f2fs_reserve_compress_blocks(filp, arg);
4033 case F2FS_IOC_SEC_TRIM_FILE: 4289 case F2FS_IOC_SEC_TRIM_FILE:
4034 return f2fs_sec_trim_file(filp, arg); 4290 return f2fs_sec_trim_file(filp, arg);
4291 case F2FS_IOC_GET_COMPRESS_OPTION:
4292 return f2fs_ioc_get_compress_option(filp, arg);
4293 case F2FS_IOC_SET_COMPRESS_OPTION:
4294 return f2fs_ioc_set_compress_option(filp, arg);
4295 case F2FS_IOC_DECOMPRESS_FILE:
4296 return f2fs_ioc_decompress_file(filp, arg);
4297 case F2FS_IOC_COMPRESS_FILE:
4298 return f2fs_ioc_compress_file(filp, arg);
4035 default: 4299 default:
4036 return -ENOTTY; 4300 return -ENOTTY;
4037 } 4301 }
4038} 4302}
4039 4303
4304long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4305{
4306 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4307 return -EIO;
4308 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4309 return -ENOSPC;
4310
4311 return __f2fs_ioctl(filp, cmd, arg);
4312}
4313
4040static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) 4314static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
4041{ 4315{
4042 struct file *file = iocb->ki_filp; 4316 struct file *file = iocb->ki_filp;
@@ -4153,8 +4427,63 @@ out:
4153} 4427}
4154 4428
4155#ifdef CONFIG_COMPAT 4429#ifdef CONFIG_COMPAT
4430struct compat_f2fs_gc_range {
4431 u32 sync;
4432 compat_u64 start;
4433 compat_u64 len;
4434};
4435#define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
4436 struct compat_f2fs_gc_range)
4437
4438static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
4439{
4440 struct compat_f2fs_gc_range __user *urange;
4441 struct f2fs_gc_range range;
4442 int err;
4443
4444 urange = compat_ptr(arg);
4445 err = get_user(range.sync, &urange->sync);
4446 err |= get_user(range.start, &urange->start);
4447 err |= get_user(range.len, &urange->len);
4448 if (err)
4449 return -EFAULT;
4450
4451 return __f2fs_ioc_gc_range(file, &range);
4452}
4453
4454struct compat_f2fs_move_range {
4455 u32 dst_fd;
4456 compat_u64 pos_in;
4457 compat_u64 pos_out;
4458 compat_u64 len;
4459};
4460#define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
4461 struct compat_f2fs_move_range)
4462
4463static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
4464{
4465 struct compat_f2fs_move_range __user *urange;
4466 struct f2fs_move_range range;
4467 int err;
4468
4469 urange = compat_ptr(arg);
4470 err = get_user(range.dst_fd, &urange->dst_fd);
4471 err |= get_user(range.pos_in, &urange->pos_in);
4472 err |= get_user(range.pos_out, &urange->pos_out);
4473 err |= get_user(range.len, &urange->len);
4474 if (err)
4475 return -EFAULT;
4476
4477 return __f2fs_ioc_move_range(file, &range);
4478}
4479
4156long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 4480long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4157{ 4481{
4482 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
4483 return -EIO;
4484 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
4485 return -ENOSPC;
4486
4158 switch (cmd) { 4487 switch (cmd) {
4159 case FS_IOC32_GETFLAGS: 4488 case FS_IOC32_GETFLAGS:
4160 cmd = FS_IOC_GETFLAGS; 4489 cmd = FS_IOC_GETFLAGS;
@@ -4165,6 +4494,10 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4165 case FS_IOC32_GETVERSION: 4494 case FS_IOC32_GETVERSION:
4166 cmd = FS_IOC_GETVERSION; 4495 cmd = FS_IOC_GETVERSION;
4167 break; 4496 break;
4497 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
4498 return f2fs_compat_ioc_gc_range(file, arg);
4499 case F2FS_IOC32_MOVE_RANGE:
4500 return f2fs_compat_ioc_move_range(file, arg);
4168 case F2FS_IOC_START_ATOMIC_WRITE: 4501 case F2FS_IOC_START_ATOMIC_WRITE:
4169 case F2FS_IOC_COMMIT_ATOMIC_WRITE: 4502 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4170 case F2FS_IOC_START_VOLATILE_WRITE: 4503 case F2FS_IOC_START_VOLATILE_WRITE:
@@ -4182,10 +4515,8 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4182 case FS_IOC_GET_ENCRYPTION_KEY_STATUS: 4515 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4183 case FS_IOC_GET_ENCRYPTION_NONCE: 4516 case FS_IOC_GET_ENCRYPTION_NONCE:
4184 case F2FS_IOC_GARBAGE_COLLECT: 4517 case F2FS_IOC_GARBAGE_COLLECT:
4185 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4186 case F2FS_IOC_WRITE_CHECKPOINT: 4518 case F2FS_IOC_WRITE_CHECKPOINT:
4187 case F2FS_IOC_DEFRAGMENT: 4519 case F2FS_IOC_DEFRAGMENT:
4188 case F2FS_IOC_MOVE_RANGE:
4189 case F2FS_IOC_FLUSH_DEVICE: 4520 case F2FS_IOC_FLUSH_DEVICE:
4190 case F2FS_IOC_GET_FEATURES: 4521 case F2FS_IOC_GET_FEATURES:
4191 case FS_IOC_FSGETXATTR: 4522 case FS_IOC_FSGETXATTR:
@@ -4202,11 +4533,15 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4202 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS: 4533 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4203 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS: 4534 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4204 case F2FS_IOC_SEC_TRIM_FILE: 4535 case F2FS_IOC_SEC_TRIM_FILE:
4536 case F2FS_IOC_GET_COMPRESS_OPTION:
4537 case F2FS_IOC_SET_COMPRESS_OPTION:
4538 case F2FS_IOC_DECOMPRESS_FILE:
4539 case F2FS_IOC_COMPRESS_FILE:
4205 break; 4540 break;
4206 default: 4541 default:
4207 return -ENOIOCTLCMD; 4542 return -ENOIOCTLCMD;
4208 } 4543 }
4209 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 4544 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4210} 4545}
4211#endif 4546#endif
4212 4547
diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index 05641a1e36cc..3ef84e6ded41 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -1986,7 +1986,7 @@ int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count)
1986 1986
1987 freeze_super(sbi->sb); 1987 freeze_super(sbi->sb);
1988 down_write(&sbi->gc_lock); 1988 down_write(&sbi->gc_lock);
1989 mutex_lock(&sbi->cp_mutex); 1989 down_write(&sbi->cp_global_sem);
1990 1990
1991 spin_lock(&sbi->stat_lock); 1991 spin_lock(&sbi->stat_lock);
1992 if (shrunk_blocks + valid_user_blocks(sbi) + 1992 if (shrunk_blocks + valid_user_blocks(sbi) +
@@ -2031,7 +2031,7 @@ recover_out:
2031 spin_unlock(&sbi->stat_lock); 2031 spin_unlock(&sbi->stat_lock);
2032 } 2032 }
2033out_err: 2033out_err:
2034 mutex_unlock(&sbi->cp_mutex); 2034 up_write(&sbi->cp_global_sem);
2035 up_write(&sbi->gc_lock); 2035 up_write(&sbi->gc_lock);
2036 thaw_super(sbi->sb); 2036 thaw_super(sbi->sb);
2037 clear_sbi_flag(sbi, SBI_IS_RESIZEFS); 2037 clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
diff --git a/fs/f2fs/inline.c b/fs/f2fs/inline.c
index b93d0ccbf983..0a8f64feefe4 100644
--- a/fs/f2fs/inline.c
+++ b/fs/f2fs/inline.c
@@ -206,7 +206,8 @@ int f2fs_convert_inline_inode(struct inode *inode)
206 struct page *ipage, *page; 206 struct page *ipage, *page;
207 int err = 0; 207 int err = 0;
208 208
209 if (!f2fs_has_inline_data(inode)) 209 if (!f2fs_has_inline_data(inode) ||
210 f2fs_hw_is_readonly(sbi) || f2fs_readonly(sbi->sb))
210 return 0; 211 return 0;
211 212
212 page = f2fs_grab_cache_page(inode->i_mapping, 0, false); 213 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
@@ -284,7 +285,7 @@ int f2fs_recover_inline_data(struct inode *inode, struct page *npage)
284 * [prev.] [next] of inline_data flag 285 * [prev.] [next] of inline_data flag
285 * o o -> recover inline_data 286 * o o -> recover inline_data
286 * o x -> remove inline_data, and then recover data blocks 287 * o x -> remove inline_data, and then recover data blocks
287 * x o -> remove inline_data, and then recover inline_data 288 * x o -> remove data blocks, and then recover inline_data
288 * x x -> recover data blocks 289 * x x -> recover data blocks
289 */ 290 */
290 if (IS_INODE(npage)) 291 if (IS_INODE(npage))
@@ -316,6 +317,7 @@ process_inline:
316 if (IS_ERR(ipage)) 317 if (IS_ERR(ipage))
317 return PTR_ERR(ipage); 318 return PTR_ERR(ipage);
318 f2fs_truncate_inline_inode(inode, ipage, 0); 319 f2fs_truncate_inline_inode(inode, ipage, 0);
320 stat_dec_inline_inode(inode);
319 clear_inode_flag(inode, FI_INLINE_DATA); 321 clear_inode_flag(inode, FI_INLINE_DATA);
320 f2fs_put_page(ipage, 1); 322 f2fs_put_page(ipage, 1);
321 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) { 323 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
@@ -324,6 +326,7 @@ process_inline:
324 ret = f2fs_truncate_blocks(inode, 0, false); 326 ret = f2fs_truncate_blocks(inode, 0, false);
325 if (ret) 327 if (ret)
326 return ret; 328 return ret;
329 stat_inc_inline_inode(inode);
327 goto process_inline; 330 goto process_inline;
328 } 331 }
329 return 0; 332 return 0;
diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
index 657db2fb6739..349d9cb933ee 100644
--- a/fs/f2fs/inode.c
+++ b/fs/f2fs/inode.c
@@ -456,6 +456,7 @@ static int do_read_inode(struct inode *inode)
456 le64_to_cpu(ri->i_compr_blocks)); 456 le64_to_cpu(ri->i_compr_blocks));
457 fi->i_compress_algorithm = ri->i_compress_algorithm; 457 fi->i_compress_algorithm = ri->i_compress_algorithm;
458 fi->i_log_cluster_size = ri->i_log_cluster_size; 458 fi->i_log_cluster_size = ri->i_log_cluster_size;
459 fi->i_compress_flag = le16_to_cpu(ri->i_compress_flag);
459 fi->i_cluster_size = 1 << fi->i_log_cluster_size; 460 fi->i_cluster_size = 1 << fi->i_log_cluster_size;
460 set_inode_flag(inode, FI_COMPRESSED_FILE); 461 set_inode_flag(inode, FI_COMPRESSED_FILE);
461 } 462 }
@@ -634,6 +635,8 @@ void f2fs_update_inode(struct inode *inode, struct page *node_page)
634 &F2FS_I(inode)->i_compr_blocks)); 635 &F2FS_I(inode)->i_compr_blocks));
635 ri->i_compress_algorithm = 636 ri->i_compress_algorithm =
636 F2FS_I(inode)->i_compress_algorithm; 637 F2FS_I(inode)->i_compress_algorithm;
638 ri->i_compress_flag =
639 cpu_to_le16(F2FS_I(inode)->i_compress_flag);
637 ri->i_log_cluster_size = 640 ri->i_log_cluster_size =
638 F2FS_I(inode)->i_log_cluster_size; 641 F2FS_I(inode)->i_log_cluster_size;
639 } 642 }
diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
index d5d8ce077f29..3a24423ac65f 100644
--- a/fs/f2fs/node.c
+++ b/fs/f2fs/node.c
@@ -62,8 +62,8 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
62 sizeof(struct free_nid)) >> PAGE_SHIFT; 62 sizeof(struct free_nid)) >> PAGE_SHIFT;
63 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2); 63 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
64 } else if (type == NAT_ENTRIES) { 64 } else if (type == NAT_ENTRIES) {
65 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >> 65 mem_size = (nm_i->nat_cnt[TOTAL_NAT] *
66 PAGE_SHIFT; 66 sizeof(struct nat_entry)) >> PAGE_SHIFT;
67 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2); 67 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
68 if (excess_cached_nats(sbi)) 68 if (excess_cached_nats(sbi))
69 res = false; 69 res = false;
@@ -109,7 +109,7 @@ static void clear_node_page_dirty(struct page *page)
109 109
110static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid) 110static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
111{ 111{
112 return f2fs_get_meta_page(sbi, current_nat_addr(sbi, nid)); 112 return f2fs_get_meta_page_retry(sbi, current_nat_addr(sbi, nid));
113} 113}
114 114
115static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid) 115static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
@@ -177,7 +177,8 @@ static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i,
177 list_add_tail(&ne->list, &nm_i->nat_entries); 177 list_add_tail(&ne->list, &nm_i->nat_entries);
178 spin_unlock(&nm_i->nat_list_lock); 178 spin_unlock(&nm_i->nat_list_lock);
179 179
180 nm_i->nat_cnt++; 180 nm_i->nat_cnt[TOTAL_NAT]++;
181 nm_i->nat_cnt[RECLAIMABLE_NAT]++;
181 return ne; 182 return ne;
182} 183}
183 184
@@ -207,7 +208,8 @@ static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
207static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e) 208static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
208{ 209{
209 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e)); 210 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
210 nm_i->nat_cnt--; 211 nm_i->nat_cnt[TOTAL_NAT]--;
212 nm_i->nat_cnt[RECLAIMABLE_NAT]--;
211 __free_nat_entry(e); 213 __free_nat_entry(e);
212} 214}
213 215
@@ -253,7 +255,8 @@ static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
253 if (get_nat_flag(ne, IS_DIRTY)) 255 if (get_nat_flag(ne, IS_DIRTY))
254 goto refresh_list; 256 goto refresh_list;
255 257
256 nm_i->dirty_nat_cnt++; 258 nm_i->nat_cnt[DIRTY_NAT]++;
259 nm_i->nat_cnt[RECLAIMABLE_NAT]--;
257 set_nat_flag(ne, IS_DIRTY, true); 260 set_nat_flag(ne, IS_DIRTY, true);
258refresh_list: 261refresh_list:
259 spin_lock(&nm_i->nat_list_lock); 262 spin_lock(&nm_i->nat_list_lock);
@@ -273,7 +276,8 @@ static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
273 276
274 set_nat_flag(ne, IS_DIRTY, false); 277 set_nat_flag(ne, IS_DIRTY, false);
275 set->entry_cnt--; 278 set->entry_cnt--;
276 nm_i->dirty_nat_cnt--; 279 nm_i->nat_cnt[DIRTY_NAT]--;
280 nm_i->nat_cnt[RECLAIMABLE_NAT]++;
277} 281}
278 282
279static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i, 283static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
@@ -2590,9 +2594,15 @@ int f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
2590 2594
2591 ri = F2FS_INODE(page); 2595 ri = F2FS_INODE(page);
2592 if (ri->i_inline & F2FS_INLINE_XATTR) { 2596 if (ri->i_inline & F2FS_INLINE_XATTR) {
2593 set_inode_flag(inode, FI_INLINE_XATTR); 2597 if (!f2fs_has_inline_xattr(inode)) {
2598 set_inode_flag(inode, FI_INLINE_XATTR);
2599 stat_inc_inline_xattr(inode);
2600 }
2594 } else { 2601 } else {
2595 clear_inode_flag(inode, FI_INLINE_XATTR); 2602 if (f2fs_has_inline_xattr(inode)) {
2603 stat_dec_inline_xattr(inode);
2604 clear_inode_flag(inode, FI_INLINE_XATTR);
2605 }
2596 goto update_inode; 2606 goto update_inode;
2597 } 2607 }
2598 2608
@@ -2944,14 +2954,17 @@ int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
2944 LIST_HEAD(sets); 2954 LIST_HEAD(sets);
2945 int err = 0; 2955 int err = 0;
2946 2956
2947 /* during unmount, let's flush nat_bits before checking dirty_nat_cnt */ 2957 /*
2958 * during unmount, let's flush nat_bits before checking
2959 * nat_cnt[DIRTY_NAT].
2960 */
2948 if (enabled_nat_bits(sbi, cpc)) { 2961 if (enabled_nat_bits(sbi, cpc)) {
2949 down_write(&nm_i->nat_tree_lock); 2962 down_write(&nm_i->nat_tree_lock);
2950 remove_nats_in_journal(sbi); 2963 remove_nats_in_journal(sbi);
2951 up_write(&nm_i->nat_tree_lock); 2964 up_write(&nm_i->nat_tree_lock);
2952 } 2965 }
2953 2966
2954 if (!nm_i->dirty_nat_cnt) 2967 if (!nm_i->nat_cnt[DIRTY_NAT])
2955 return 0; 2968 return 0;
2956 2969
2957 down_write(&nm_i->nat_tree_lock); 2970 down_write(&nm_i->nat_tree_lock);
@@ -2962,7 +2975,8 @@ int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
2962 * into nat entry set. 2975 * into nat entry set.
2963 */ 2976 */
2964 if (enabled_nat_bits(sbi, cpc) || 2977 if (enabled_nat_bits(sbi, cpc) ||
2965 !__has_cursum_space(journal, nm_i->dirty_nat_cnt, NAT_JOURNAL)) 2978 !__has_cursum_space(journal,
2979 nm_i->nat_cnt[DIRTY_NAT], NAT_JOURNAL))
2966 remove_nats_in_journal(sbi); 2980 remove_nats_in_journal(sbi);
2967 2981
2968 while ((found = __gang_lookup_nat_set(nm_i, 2982 while ((found = __gang_lookup_nat_set(nm_i,
@@ -3086,7 +3100,6 @@ static int init_node_manager(struct f2fs_sb_info *sbi)
3086 F2FS_RESERVED_NODE_NUM; 3100 F2FS_RESERVED_NODE_NUM;
3087 nm_i->nid_cnt[FREE_NID] = 0; 3101 nm_i->nid_cnt[FREE_NID] = 0;
3088 nm_i->nid_cnt[PREALLOC_NID] = 0; 3102 nm_i->nid_cnt[PREALLOC_NID] = 0;
3089 nm_i->nat_cnt = 0;
3090 nm_i->ram_thresh = DEF_RAM_THRESHOLD; 3103 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
3091 nm_i->ra_nid_pages = DEF_RA_NID_PAGES; 3104 nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
3092 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD; 3105 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
@@ -3220,7 +3233,7 @@ void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
3220 __del_from_nat_cache(nm_i, natvec[idx]); 3233 __del_from_nat_cache(nm_i, natvec[idx]);
3221 } 3234 }
3222 } 3235 }
3223 f2fs_bug_on(sbi, nm_i->nat_cnt); 3236 f2fs_bug_on(sbi, nm_i->nat_cnt[TOTAL_NAT]);
3224 3237
3225 /* destroy nat set cache */ 3238 /* destroy nat set cache */
3226 nid = 0; 3239 nid = 0;
diff --git a/fs/f2fs/node.h b/fs/f2fs/node.h
index 69e5859e993c..f84541b57acb 100644
--- a/fs/f2fs/node.h
+++ b/fs/f2fs/node.h
@@ -126,13 +126,13 @@ static inline void raw_nat_from_node_info(struct f2fs_nat_entry *raw_ne,
126 126
127static inline bool excess_dirty_nats(struct f2fs_sb_info *sbi) 127static inline bool excess_dirty_nats(struct f2fs_sb_info *sbi)
128{ 128{
129 return NM_I(sbi)->dirty_nat_cnt >= NM_I(sbi)->max_nid * 129 return NM_I(sbi)->nat_cnt[DIRTY_NAT] >= NM_I(sbi)->max_nid *
130 NM_I(sbi)->dirty_nats_ratio / 100; 130 NM_I(sbi)->dirty_nats_ratio / 100;
131} 131}
132 132
133static inline bool excess_cached_nats(struct f2fs_sb_info *sbi) 133static inline bool excess_cached_nats(struct f2fs_sb_info *sbi)
134{ 134{
135 return NM_I(sbi)->nat_cnt >= DEF_NAT_CACHE_THRESHOLD; 135 return NM_I(sbi)->nat_cnt[TOTAL_NAT] >= DEF_NAT_CACHE_THRESHOLD;
136} 136}
137 137
138static inline bool excess_dirty_nodes(struct f2fs_sb_info *sbi) 138static inline bool excess_dirty_nodes(struct f2fs_sb_info *sbi)
diff --git a/fs/f2fs/recovery.c b/fs/f2fs/recovery.c
index 0947d36af1a8..da75d5d52f0a 100644
--- a/fs/f2fs/recovery.c
+++ b/fs/f2fs/recovery.c
@@ -799,7 +799,7 @@ int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
799 INIT_LIST_HEAD(&dir_list); 799 INIT_LIST_HEAD(&dir_list);
800 800
801 /* prevent checkpoint */ 801 /* prevent checkpoint */
802 mutex_lock(&sbi->cp_mutex); 802 down_write(&sbi->cp_global_sem);
803 803
804 /* step #1: find fsynced inode numbers */ 804 /* step #1: find fsynced inode numbers */
805 err = find_fsync_dnodes(sbi, &inode_list, check_only); 805 err = find_fsync_dnodes(sbi, &inode_list, check_only);
@@ -850,7 +850,7 @@ skip:
850 if (!err) 850 if (!err)
851 clear_sbi_flag(sbi, SBI_POR_DOING); 851 clear_sbi_flag(sbi, SBI_POR_DOING);
852 852
853 mutex_unlock(&sbi->cp_mutex); 853 up_write(&sbi->cp_global_sem);
854 854
855 /* let's drop all the directory inodes for clean checkpoint */ 855 /* let's drop all the directory inodes for clean checkpoint */
856 destroy_fsync_dnodes(&dir_list, err); 856 destroy_fsync_dnodes(&dir_list, err);
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index f2a4265318f5..deca74cb17df 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -529,31 +529,38 @@ void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg)
529 else 529 else
530 f2fs_build_free_nids(sbi, false, false); 530 f2fs_build_free_nids(sbi, false, false);
531 531
532 if (!is_idle(sbi, REQ_TIME) && 532 if (excess_dirty_nats(sbi) || excess_dirty_nodes(sbi) ||
533 (!excess_dirty_nats(sbi) && !excess_dirty_nodes(sbi))) 533 excess_prefree_segs(sbi))
534 goto do_sync;
535
536 /* there is background inflight IO or foreground operation recently */
537 if (is_inflight_io(sbi, REQ_TIME) ||
538 (!f2fs_time_over(sbi, REQ_TIME) && rwsem_is_locked(&sbi->cp_rwsem)))
534 return; 539 return;
535 540
541 /* exceed periodical checkpoint timeout threshold */
542 if (f2fs_time_over(sbi, CP_TIME))
543 goto do_sync;
544
536 /* checkpoint is the only way to shrink partial cached entries */ 545 /* checkpoint is the only way to shrink partial cached entries */
537 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES) || 546 if (f2fs_available_free_memory(sbi, NAT_ENTRIES) ||
538 !f2fs_available_free_memory(sbi, INO_ENTRIES) || 547 f2fs_available_free_memory(sbi, INO_ENTRIES))
539 excess_prefree_segs(sbi) || 548 return;
540 excess_dirty_nats(sbi) ||
541 excess_dirty_nodes(sbi) ||
542 f2fs_time_over(sbi, CP_TIME)) {
543 if (test_opt(sbi, DATA_FLUSH) && from_bg) {
544 struct blk_plug plug;
545
546 mutex_lock(&sbi->flush_lock);
547
548 blk_start_plug(&plug);
549 f2fs_sync_dirty_inodes(sbi, FILE_INODE);
550 blk_finish_plug(&plug);
551 549
552 mutex_unlock(&sbi->flush_lock); 550do_sync:
553 } 551 if (test_opt(sbi, DATA_FLUSH) && from_bg) {
554 f2fs_sync_fs(sbi->sb, true); 552 struct blk_plug plug;
555 stat_inc_bg_cp_count(sbi->stat_info); 553
554 mutex_lock(&sbi->flush_lock);
555
556 blk_start_plug(&plug);
557 f2fs_sync_dirty_inodes(sbi, FILE_INODE);
558 blk_finish_plug(&plug);
559
560 mutex_unlock(&sbi->flush_lock);
556 } 561 }
562 f2fs_sync_fs(sbi->sb, true);
563 stat_inc_bg_cp_count(sbi->stat_info);
557} 564}
558 565
559static int __submit_flush_wait(struct f2fs_sb_info *sbi, 566static int __submit_flush_wait(struct f2fs_sb_info *sbi,
@@ -3254,7 +3261,7 @@ static int __get_segment_type_6(struct f2fs_io_info *fio)
3254 else 3261 else
3255 return CURSEG_COLD_DATA; 3262 return CURSEG_COLD_DATA;
3256 } 3263 }
3257 if (file_is_cold(inode) || f2fs_compressed_file(inode)) 3264 if (file_is_cold(inode) || f2fs_need_compress_data(inode))
3258 return CURSEG_COLD_DATA; 3265 return CURSEG_COLD_DATA;
3259 if (file_is_hot(inode) || 3266 if (file_is_hot(inode) ||
3260 is_inode_flag_set(inode, FI_HOT_DATA) || 3267 is_inode_flag_set(inode, FI_HOT_DATA) ||
diff --git a/fs/f2fs/shrinker.c b/fs/f2fs/shrinker.c
index d66de5999a26..dd3c3c7a90ec 100644
--- a/fs/f2fs/shrinker.c
+++ b/fs/f2fs/shrinker.c
@@ -18,9 +18,7 @@ static unsigned int shrinker_run_no;
18 18
19static unsigned long __count_nat_entries(struct f2fs_sb_info *sbi) 19static unsigned long __count_nat_entries(struct f2fs_sb_info *sbi)
20{ 20{
21 long count = NM_I(sbi)->nat_cnt - NM_I(sbi)->dirty_nat_cnt; 21 return NM_I(sbi)->nat_cnt[RECLAIMABLE_NAT];
22
23 return count > 0 ? count : 0;
24} 22}
25 23
26static unsigned long __count_free_nids(struct f2fs_sb_info *sbi) 24static unsigned long __count_free_nids(struct f2fs_sb_info *sbi)
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 42293b7ceaf2..e8b94bb14bd8 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -146,6 +146,8 @@ enum {
146 Opt_compress_algorithm, 146 Opt_compress_algorithm,
147 Opt_compress_log_size, 147 Opt_compress_log_size,
148 Opt_compress_extension, 148 Opt_compress_extension,
149 Opt_compress_chksum,
150 Opt_compress_mode,
149 Opt_atgc, 151 Opt_atgc,
150 Opt_err, 152 Opt_err,
151}; 153};
@@ -214,6 +216,8 @@ static match_table_t f2fs_tokens = {
214 {Opt_compress_algorithm, "compress_algorithm=%s"}, 216 {Opt_compress_algorithm, "compress_algorithm=%s"},
215 {Opt_compress_log_size, "compress_log_size=%u"}, 217 {Opt_compress_log_size, "compress_log_size=%u"},
216 {Opt_compress_extension, "compress_extension=%s"}, 218 {Opt_compress_extension, "compress_extension=%s"},
219 {Opt_compress_chksum, "compress_chksum"},
220 {Opt_compress_mode, "compress_mode=%s"},
217 {Opt_atgc, "atgc"}, 221 {Opt_atgc, "atgc"},
218 {Opt_err, NULL}, 222 {Opt_err, NULL},
219}; 223};
@@ -934,10 +938,29 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
934 F2FS_OPTION(sbi).compress_ext_cnt++; 938 F2FS_OPTION(sbi).compress_ext_cnt++;
935 kfree(name); 939 kfree(name);
936 break; 940 break;
941 case Opt_compress_chksum:
942 F2FS_OPTION(sbi).compress_chksum = true;
943 break;
944 case Opt_compress_mode:
945 name = match_strdup(&args[0]);
946 if (!name)
947 return -ENOMEM;
948 if (!strcmp(name, "fs")) {
949 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
950 } else if (!strcmp(name, "user")) {
951 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_USER;
952 } else {
953 kfree(name);
954 return -EINVAL;
955 }
956 kfree(name);
957 break;
937#else 958#else
938 case Opt_compress_algorithm: 959 case Opt_compress_algorithm:
939 case Opt_compress_log_size: 960 case Opt_compress_log_size:
940 case Opt_compress_extension: 961 case Opt_compress_extension:
962 case Opt_compress_chksum:
963 case Opt_compress_mode:
941 f2fs_info(sbi, "compression options not supported"); 964 f2fs_info(sbi, "compression options not supported");
942 break; 965 break;
943#endif 966#endif
@@ -1523,6 +1546,14 @@ static inline void f2fs_show_compress_options(struct seq_file *seq,
1523 seq_printf(seq, ",compress_extension=%s", 1546 seq_printf(seq, ",compress_extension=%s",
1524 F2FS_OPTION(sbi).extensions[i]); 1547 F2FS_OPTION(sbi).extensions[i]);
1525 } 1548 }
1549
1550 if (F2FS_OPTION(sbi).compress_chksum)
1551 seq_puts(seq, ",compress_chksum");
1552
1553 if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
1554 seq_printf(seq, ",compress_mode=%s", "fs");
1555 else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
1556 seq_printf(seq, ",compress_mode=%s", "user");
1526} 1557}
1527 1558
1528static int f2fs_show_options(struct seq_file *seq, struct dentry *root) 1559static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
@@ -1672,6 +1703,7 @@ static void default_options(struct f2fs_sb_info *sbi)
1672 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4; 1703 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
1673 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE; 1704 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
1674 F2FS_OPTION(sbi).compress_ext_cnt = 0; 1705 F2FS_OPTION(sbi).compress_ext_cnt = 0;
1706 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1675 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON; 1707 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
1676 1708
1677 sbi->sb->s_flags &= ~SB_INLINECRYPT; 1709 sbi->sb->s_flags &= ~SB_INLINECRYPT;
@@ -1904,7 +1936,6 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1904 1936
1905 if (*flags & SB_RDONLY || 1937 if (*flags & SB_RDONLY ||
1906 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) { 1938 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
1907 writeback_inodes_sb(sb, WB_REASON_SYNC);
1908 sync_inodes_sb(sb); 1939 sync_inodes_sb(sb);
1909 1940
1910 set_sbi_flag(sbi, SBI_IS_DIRTY); 1941 set_sbi_flag(sbi, SBI_IS_DIRTY);
@@ -2744,7 +2775,6 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
2744 block_t total_sections, blocks_per_seg; 2775 block_t total_sections, blocks_per_seg;
2745 struct f2fs_super_block *raw_super = (struct f2fs_super_block *) 2776 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2746 (bh->b_data + F2FS_SUPER_OFFSET); 2777 (bh->b_data + F2FS_SUPER_OFFSET);
2747 unsigned int blocksize;
2748 size_t crc_offset = 0; 2778 size_t crc_offset = 0;
2749 __u32 crc = 0; 2779 __u32 crc = 0;
2750 2780
@@ -2770,18 +2800,11 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
2770 } 2800 }
2771 } 2801 }
2772 2802
2773 /* Currently, support only 4KB page cache size */
2774 if (F2FS_BLKSIZE != PAGE_SIZE) {
2775 f2fs_info(sbi, "Invalid page_cache_size (%lu), supports only 4KB",
2776 PAGE_SIZE);
2777 return -EFSCORRUPTED;
2778 }
2779
2780 /* Currently, support only 4KB block size */ 2803 /* Currently, support only 4KB block size */
2781 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize); 2804 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
2782 if (blocksize != F2FS_BLKSIZE) { 2805 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
2783 f2fs_info(sbi, "Invalid blocksize (%u), supports only 4KB", 2806 le32_to_cpu(raw_super->log_blocksize),
2784 blocksize); 2807 F2FS_BLKSIZE_BITS);
2785 return -EFSCORRUPTED; 2808 return -EFSCORRUPTED;
2786 } 2809 }
2787 2810
@@ -3071,9 +3094,9 @@ static void init_sb_info(struct f2fs_sb_info *sbi)
3071 sbi->total_node_count = 3094 sbi->total_node_count =
3072 (le32_to_cpu(raw_super->segment_count_nat) / 2) 3095 (le32_to_cpu(raw_super->segment_count_nat) / 2)
3073 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK; 3096 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
3074 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino); 3097 F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
3075 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino); 3098 F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
3076 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino); 3099 F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
3077 sbi->cur_victim_sec = NULL_SECNO; 3100 sbi->cur_victim_sec = NULL_SECNO;
3078 sbi->next_victim_seg[BG_GC] = NULL_SEGNO; 3101 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
3079 sbi->next_victim_seg[FG_GC] = NULL_SEGNO; 3102 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
@@ -3552,7 +3575,7 @@ try_onemore:
3552 sbi->valid_super_block = valid_super_block; 3575 sbi->valid_super_block = valid_super_block;
3553 init_rwsem(&sbi->gc_lock); 3576 init_rwsem(&sbi->gc_lock);
3554 mutex_init(&sbi->writepages); 3577 mutex_init(&sbi->writepages);
3555 mutex_init(&sbi->cp_mutex); 3578 init_rwsem(&sbi->cp_global_sem);
3556 init_rwsem(&sbi->node_write); 3579 init_rwsem(&sbi->node_write);
3557 init_rwsem(&sbi->node_change); 3580 init_rwsem(&sbi->node_change);
3558 3581
@@ -3693,10 +3716,7 @@ try_onemore:
3693 } 3716 }
3694 3717
3695 /* For write statistics */ 3718 /* For write statistics */
3696 if (sb->s_bdev->bd_part) 3719 sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
3697 sbi->sectors_written_start =
3698 (u64)part_stat_read(sb->s_bdev->bd_part,
3699 sectors[STAT_WRITE]);
3700 3720
3701 /* Read accumulated write IO statistics if exists */ 3721 /* Read accumulated write IO statistics if exists */
3702 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 3722 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
@@ -3911,6 +3931,7 @@ free_bio_info:
3911 3931
3912#ifdef CONFIG_UNICODE 3932#ifdef CONFIG_UNICODE
3913 utf8_unload(sb->s_encoding); 3933 utf8_unload(sb->s_encoding);
3934 sb->s_encoding = NULL;
3914#endif 3935#endif
3915free_options: 3936free_options:
3916#ifdef CONFIG_QUOTA 3937#ifdef CONFIG_QUOTA
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index ec77ccfea923..989a649cfa8b 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -97,7 +97,8 @@ static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
97 97
98 return sprintf(buf, "%llu\n", 98 return sprintf(buf, "%llu\n",
99 (unsigned long long)(sbi->kbytes_written + 99 (unsigned long long)(sbi->kbytes_written +
100 BD_PART_WRITTEN(sbi))); 100 ((f2fs_get_sectors_written(sbi) -
101 sbi->sectors_written_start) >> 1)));
101} 102}
102 103
103static ssize_t features_show(struct f2fs_attr *a, 104static ssize_t features_show(struct f2fs_attr *a,
@@ -566,6 +567,7 @@ F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
566F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable); 567F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
567F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms); 568F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
568F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra); 569F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
570F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_io_bytes, max_io_bytes);
569F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold); 571F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
570F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list); 572F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
571#ifdef CONFIG_F2FS_FAULT_INJECTION 573#ifdef CONFIG_F2FS_FAULT_INJECTION
@@ -650,6 +652,7 @@ static struct attribute *f2fs_attrs[] = {
650 ATTR_LIST(iostat_enable), 652 ATTR_LIST(iostat_enable),
651 ATTR_LIST(iostat_period_ms), 653 ATTR_LIST(iostat_period_ms),
652 ATTR_LIST(readdir_ra), 654 ATTR_LIST(readdir_ra),
655 ATTR_LIST(max_io_bytes),
653 ATTR_LIST(gc_pin_file_thresh), 656 ATTR_LIST(gc_pin_file_thresh),
654 ATTR_LIST(extension_list), 657 ATTR_LIST(extension_list),
655#ifdef CONFIG_F2FS_FAULT_INJECTION 658#ifdef CONFIG_F2FS_FAULT_INJECTION
diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
index a5dbb57a687f..7dc2a06cf19a 100644
--- a/include/linux/f2fs_fs.h
+++ b/include/linux/f2fs_fs.h
@@ -273,7 +273,7 @@ struct f2fs_inode {
273 __le64 i_compr_blocks; /* # of compressed blocks */ 273 __le64 i_compr_blocks; /* # of compressed blocks */
274 __u8 i_compress_algorithm; /* compress algorithm */ 274 __u8 i_compress_algorithm; /* compress algorithm */
275 __u8 i_log_cluster_size; /* log of cluster size */ 275 __u8 i_log_cluster_size; /* log of cluster size */
276 __le16 i_padding; /* padding */ 276 __le16 i_compress_flag; /* compress flag */
277 __le32 i_extra_end[0]; /* for attribute size calculation */ 277 __le32 i_extra_end[0]; /* for attribute size calculation */
278 } __packed; 278 } __packed;
279 __le32 i_addr[DEF_ADDRS_PER_INODE]; /* Pointers to data blocks */ 279 __le32 i_addr[DEF_ADDRS_PER_INODE]; /* Pointers to data blocks */
diff --git a/include/trace/events/f2fs.h b/include/trace/events/f2fs.h
index f8f1e85ff130..56b113e3cd6a 100644
--- a/include/trace/events/f2fs.h
+++ b/include/trace/events/f2fs.h
@@ -6,6 +6,7 @@
6#define _TRACE_F2FS_H 6#define _TRACE_F2FS_H
7 7
8#include <linux/tracepoint.h> 8#include <linux/tracepoint.h>
9#include <uapi/linux/f2fs.h>
9 10
10#define show_dev(dev) MAJOR(dev), MINOR(dev) 11#define show_dev(dev) MAJOR(dev), MINOR(dev)
11#define show_dev_ino(entry) show_dev(entry->dev), (unsigned long)entry->ino 12#define show_dev_ino(entry) show_dev(entry->dev), (unsigned long)entry->ino
diff --git a/include/uapi/linux/f2fs.h b/include/uapi/linux/f2fs.h
new file mode 100644
index 000000000000..352a822d4370
--- /dev/null
+++ b/include/uapi/linux/f2fs.h
@@ -0,0 +1,98 @@
1/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2
3#ifndef _UAPI_LINUX_F2FS_H
4#define _UAPI_LINUX_F2FS_H
5#include <linux/types.h>
6#include <linux/ioctl.h>
7
8/*
9 * f2fs-specific ioctl commands
10 */
11#define F2FS_IOCTL_MAGIC 0xf5
12#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
13#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
14#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
15#define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
16#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
17#define F2FS_IOC_GARBAGE_COLLECT _IOW(F2FS_IOCTL_MAGIC, 6, __u32)
18#define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
19#define F2FS_IOC_DEFRAGMENT _IOWR(F2FS_IOCTL_MAGIC, 8, \
20 struct f2fs_defragment)
21#define F2FS_IOC_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
22 struct f2fs_move_range)
23#define F2FS_IOC_FLUSH_DEVICE _IOW(F2FS_IOCTL_MAGIC, 10, \
24 struct f2fs_flush_device)
25#define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \
26 struct f2fs_gc_range)
27#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, __u32)
28#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
29#define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
30#define F2FS_IOC_PRECACHE_EXTENTS _IO(F2FS_IOCTL_MAGIC, 15)
31#define F2FS_IOC_RESIZE_FS _IOW(F2FS_IOCTL_MAGIC, 16, __u64)
32#define F2FS_IOC_GET_COMPRESS_BLOCKS _IOR(F2FS_IOCTL_MAGIC, 17, __u64)
33#define F2FS_IOC_RELEASE_COMPRESS_BLOCKS \
34 _IOR(F2FS_IOCTL_MAGIC, 18, __u64)
35#define F2FS_IOC_RESERVE_COMPRESS_BLOCKS \
36 _IOR(F2FS_IOCTL_MAGIC, 19, __u64)
37#define F2FS_IOC_SEC_TRIM_FILE _IOW(F2FS_IOCTL_MAGIC, 20, \
38 struct f2fs_sectrim_range)
39#define F2FS_IOC_GET_COMPRESS_OPTION _IOR(F2FS_IOCTL_MAGIC, 21, \
40 struct f2fs_comp_option)
41#define F2FS_IOC_SET_COMPRESS_OPTION _IOW(F2FS_IOCTL_MAGIC, 22, \
42 struct f2fs_comp_option)
43#define F2FS_IOC_DECOMPRESS_FILE _IO(F2FS_IOCTL_MAGIC, 23)
44#define F2FS_IOC_COMPRESS_FILE _IO(F2FS_IOCTL_MAGIC, 24)
45
46/*
47 * should be same as XFS_IOC_GOINGDOWN.
48 * Flags for going down operation used by FS_IOC_GOINGDOWN
49 */
50#define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
51#define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
52#define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
53#define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
54#define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
55#define F2FS_GOING_DOWN_NEED_FSCK 0x4 /* going down to trigger fsck */
56
57/*
58 * Flags used by F2FS_IOC_SEC_TRIM_FILE
59 */
60#define F2FS_TRIM_FILE_DISCARD 0x1 /* send discard command */
61#define F2FS_TRIM_FILE_ZEROOUT 0x2 /* zero out */
62#define F2FS_TRIM_FILE_MASK 0x3
63
64struct f2fs_gc_range {
65 __u32 sync;
66 __u64 start;
67 __u64 len;
68};
69
70struct f2fs_defragment {
71 __u64 start;
72 __u64 len;
73};
74
75struct f2fs_move_range {
76 __u32 dst_fd; /* destination fd */
77 __u64 pos_in; /* start position in src_fd */
78 __u64 pos_out; /* start position in dst_fd */
79 __u64 len; /* size to move */
80};
81
82struct f2fs_flush_device {
83 __u32 dev_num; /* device number to flush */
84 __u32 segments; /* # of segments to flush */
85};
86
87struct f2fs_sectrim_range {
88 __u64 start;
89 __u64 len;
90 __u64 flags;
91};
92
93struct f2fs_comp_option {
94 __u8 algorithm;
95 __u8 log_cluster_size;
96};
97
98#endif /* _UAPI_LINUX_F2FS_H */