From b457fd79f980b82c99878edd0240d823a6a520ce Mon Sep 17 00:00:00 2001 From: "Matthew Wilcox (Oracle)" Date: Tue, 16 Jun 2020 14:31:47 +0900 Subject: [PATCH 01/14] fs: convert mpage_readpages to mpage_readahead Implement the new readahead aop and convert all callers (block_dev, exfat, ext2, fat, gfs2, hpfs, isofs, jfs, nilfs2, ocfs2, omfs, qnx6, reiserfs & udf). The callers are all trivial except for GFS2 & OCFS2. Signed-off-by: Matthew Wilcox (Oracle) Signed-off-by: Andrew Morton Reviewed-by: Junxiao Bi # ocfs2 Reviewed-by: Joseph Qi # ocfs2 Reviewed-by: Dave Chinner Reviewed-by: John Hubbard Reviewed-by: Christoph Hellwig Reviewed-by: William Kucharski Cc: Chao Yu Cc: Cong Wang Cc: Darrick J. Wong Cc: Eric Biggers Cc: Gao Xiang Cc: Jaegeuk Kim Cc: Michal Hocko Cc: Zi Yan Cc: Johannes Thumshirn Cc: Miklos Szeredi Link: http://lkml.kernel.org/r/20200414150233.24495-17-willy@infradead.org Signed-off-by: Linus Torvalds Signed-off-by: Namjae Jeon --- inode.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/inode.c b/inode.c index 9bc472d894ca..d8beb13731bd 100644 --- a/inode.c +++ b/inode.c @@ -365,11 +365,18 @@ static int exfat_readpage(struct file *file, struct page *page) return mpage_readpage(page, exfat_get_block); } +#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) +static void exfat_readahead(struct readahead_control *rac) +{ + mpage_readahead(rac, exfat_get_block); +} +#else static int exfat_readpages(struct file *file, struct address_space *mapping, struct list_head *pages, unsigned int nr_pages) { return mpage_readpages(mapping, pages, nr_pages, exfat_get_block); } +#endif static int exfat_writepage(struct page *page, struct writeback_control *wbc) { @@ -509,7 +516,11 @@ int exfat_block_truncate_page(struct inode *inode, loff_t from) static const struct address_space_operations exfat_aops = { .readpage = exfat_readpage, +#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) + .readahead = exfat_readahead, +#else .readpages = exfat_readpages, +#endif .writepage = exfat_writepage, .writepages = exfat_writepages, .write_begin = exfat_write_begin, From 117088f39d52736ab35bbe6a572faf89763e5e67 Mon Sep 17 00:00:00 2001 From: Tetsuhiro Kohada Date: Tue, 16 Jun 2020 14:25:07 +0900 Subject: [PATCH 02/14] exfat: remove EXFAT_SB_DIRTY flag This flag is set/reset in exfat_put_super()/exfat_sync_fs() to avoid sync_blockdev(). - exfat_put_super(): Before calling this, the VFS has already called sync_filesystem(), so sync is never performed here. - exfat_sync_fs(): After calling this, the VFS calls sync_blockdev(), so, it is meaningless to check EXFAT_SB_DIRTY or to bypass sync_blockdev() here. Remove the EXFAT_SB_DIRTY check to ensure synchronization. And remove the code related to the flag. Signed-off-by: Tetsuhiro Kohada Reviewed-by: Sungjong Seo Signed-off-by: Namjae Jeon --- balloc.c | 4 ++-- dir.c | 16 ++++++++-------- exfat_fs.h | 5 +---- fatent.c | 9 +++------ misc.c | 3 +-- namei.c | 12 ++++++------ super.c | 14 ++++++-------- 7 files changed, 27 insertions(+), 36 deletions(-) diff --git a/balloc.c b/balloc.c index 4055eb00ea9b..a987919686c0 100644 --- a/balloc.c +++ b/balloc.c @@ -158,7 +158,7 @@ int exfat_set_bitmap(struct inode *inode, unsigned int clu) b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx); set_bit_le(b, sbi->vol_amap[i]->b_data); - exfat_update_bh(sb, sbi->vol_amap[i], IS_DIRSYNC(inode)); + exfat_update_bh(sbi->vol_amap[i], IS_DIRSYNC(inode)); return 0; } @@ -180,7 +180,7 @@ void exfat_clear_bitmap(struct inode *inode, unsigned int clu) b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx); clear_bit_le(b, sbi->vol_amap[i]->b_data); - exfat_update_bh(sb, sbi->vol_amap[i], IS_DIRSYNC(inode)); + exfat_update_bh(sbi->vol_amap[i], IS_DIRSYNC(inode)); if (opts->discard) { int ret_discard; diff --git a/dir.c b/dir.c index 972d976bb19f..6ef56328e507 100644 --- a/dir.c +++ b/dir.c @@ -475,7 +475,7 @@ int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir, &ep->dentry.file.access_date, NULL); - exfat_update_bh(sb, bh, IS_DIRSYNC(inode)); + exfat_update_bh(bh, IS_DIRSYNC(inode)); brelse(bh); ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or); @@ -485,7 +485,7 @@ int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir, exfat_init_stream_entry(ep, (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN, start_clu, size); - exfat_update_bh(sb, bh, IS_DIRSYNC(inode)); + exfat_update_bh(bh, IS_DIRSYNC(inode)); brelse(bh); return 0; @@ -521,7 +521,7 @@ int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir, } fep->dentry.file.checksum = cpu_to_le16(chksum); - exfat_update_bh(sb, fbh, IS_DIRSYNC(inode)); + exfat_update_bh(fbh, IS_DIRSYNC(inode)); release_fbh: brelse(fbh); return ret; @@ -543,7 +543,7 @@ int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir, return -EIO; ep->dentry.file.num_ext = (unsigned char)(num_entries - 1); - exfat_update_bh(sb, bh, sync); + exfat_update_bh(bh, sync); brelse(bh); ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or); @@ -552,7 +552,7 @@ int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir, ep->dentry.stream.name_len = p_uniname->name_len; ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash); - exfat_update_bh(sb, bh, sync); + exfat_update_bh(bh, sync); brelse(bh); for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) { @@ -561,7 +561,7 @@ int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir, return -EIO; exfat_init_name_entry(ep, uniname); - exfat_update_bh(sb, bh, sync); + exfat_update_bh(bh, sync); brelse(bh); uniname += EXFAT_FILE_NAME_LEN; } @@ -585,7 +585,7 @@ int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir, return -EIO; exfat_set_entry_type(ep, TYPE_DELETED); - exfat_update_bh(sb, bh, IS_DIRSYNC(inode)); + exfat_update_bh(bh, IS_DIRSYNC(inode)); brelse(bh); } @@ -615,7 +615,7 @@ void exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync) for (i = 0; i < es->num_bh; i++) { if (es->modified) - exfat_update_bh(es->sb, es->bh[i], sync); + exfat_update_bh(es->bh[i], sync); brelse(es->bh[i]); } kfree(es); diff --git a/exfat_fs.h b/exfat_fs.h index b6ad9dd3b23d..21653b5d8507 100644 --- a/exfat_fs.h +++ b/exfat_fs.h @@ -16,8 +16,6 @@ #define EXFAT_SUPER_MAGIC 0x2011BAB0UL #define EXFAT_ROOT_INO 1 -#define EXFAT_SB_DIRTY 0 - #define EXFAT_CLUSTERS_UNTRACKED (~0u) /* @@ -241,7 +239,6 @@ struct exfat_sb_info { unsigned int clu_srch_ptr; /* cluster search pointer */ unsigned int used_clusters; /* number of used clusters */ - unsigned long s_state; struct mutex s_lock; /* superblock lock */ struct exfat_mount_options options; struct nls_table *nls_io; /* Charset used for input and display */ @@ -523,7 +520,7 @@ void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, u8 *tz, __le16 *time, __le16 *date, u8 *time_cs); u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type); u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type); -void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync); +void exfat_update_bh(struct buffer_head *bh, int sync); void exfat_chain_set(struct exfat_chain *ec, unsigned int dir, unsigned int size, unsigned char flags); void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec); diff --git a/fatent.c b/fatent.c index c264eda3c27b..8de33b8c190f 100644 --- a/fatent.c +++ b/fatent.c @@ -80,9 +80,9 @@ int exfat_ent_set(struct super_block *sb, unsigned int loc, fat_entry = (__le32 *)&(bh->b_data[off]); *fat_entry = cpu_to_le32(content); #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) - exfat_update_bh(sb, bh, sb->s_flags & SB_SYNCHRONOUS); + exfat_update_bh(bh, sb->s_flags & SB_SYNCHRONOUS); #else - exfat_update_bh(sb, bh, sb->s_flags & MS_SYNCHRONOUS); + exfat_update_bh(bh, sb->s_flags & MS_SYNCHRONOUS); #endif exfat_mirror_bh(sb, sec, bh); brelse(bh); @@ -182,7 +182,6 @@ int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain) return -EIO; } - set_bit(EXFAT_SB_DIRTY, &sbi->s_state); clu = p_chain->dir; if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { @@ -282,7 +281,7 @@ int exfat_zeroed_cluster(struct inode *dir, unsigned int clu) goto release_bhs; } memset(bhs[n]->b_data, 0, sb->s_blocksize); - exfat_update_bh(sb, bhs[n], 0); + exfat_update_bh(bhs[n], 0); n++; blknr++; @@ -366,8 +365,6 @@ int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, } } - set_bit(EXFAT_SB_DIRTY, &sbi->s_state); - p_chain->dir = EXFAT_EOF_CLUSTER; while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) != diff --git a/misc.c b/misc.c index c61ead46b428..015ac0af9280 100644 --- a/misc.c +++ b/misc.c @@ -169,9 +169,8 @@ u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type) return chksum; } -void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync) +void exfat_update_bh(struct buffer_head *bh, int sync) { - set_bit(EXFAT_SB_DIRTY, &EXFAT_SB(sb)->s_state); set_buffer_uptodate(bh); mark_buffer_dirty(bh); diff --git a/namei.c b/namei.c index f278386014d2..aea7a661e0e1 100644 --- a/namei.c +++ b/namei.c @@ -414,7 +414,7 @@ static int exfat_find_empty_entry(struct inode *inode, ep->dentry.stream.valid_size = cpu_to_le64(size); ep->dentry.stream.size = ep->dentry.stream.valid_size; ep->dentry.stream.flags = p_dir->flags; - exfat_update_bh(sb, bh, IS_DIRSYNC(inode)); + exfat_update_bh(bh, IS_DIRSYNC(inode)); brelse(bh); if (exfat_update_dir_chksum(inode, &(ei->dir), ei->entry)) @@ -1178,7 +1178,7 @@ static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir, epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE); ei->attr |= ATTR_ARCHIVE; } - exfat_update_bh(sb, new_bh, sync); + exfat_update_bh(new_bh, sync); brelse(old_bh); brelse(new_bh); @@ -1194,7 +1194,7 @@ static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir, } memcpy(epnew, epold, DENTRY_SIZE); - exfat_update_bh(sb, new_bh, sync); + exfat_update_bh(new_bh, sync); brelse(old_bh); brelse(new_bh); @@ -1211,7 +1211,7 @@ static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir, epold->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE); ei->attr |= ATTR_ARCHIVE; } - exfat_update_bh(sb, old_bh, sync); + exfat_update_bh(old_bh, sync); brelse(old_bh); ret = exfat_init_ext_entry(inode, p_dir, oldentry, num_new_entries, p_uniname); @@ -1266,7 +1266,7 @@ static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir, epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE); ei->attr |= ATTR_ARCHIVE; } - exfat_update_bh(sb, new_bh, IS_DIRSYNC(inode)); + exfat_update_bh(new_bh, IS_DIRSYNC(inode)); brelse(mov_bh); brelse(new_bh); @@ -1282,7 +1282,7 @@ static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir, } memcpy(epnew, epmov, DENTRY_SIZE); - exfat_update_bh(sb, new_bh, IS_DIRSYNC(inode)); + exfat_update_bh(new_bh, IS_DIRSYNC(inode)); brelse(mov_bh); brelse(new_bh); diff --git a/super.c b/super.c index 5cbc2707b7b0..6c1f2e7018e2 100644 --- a/super.c +++ b/super.c @@ -58,9 +58,6 @@ static void exfat_put_super(struct super_block *sb) struct exfat_sb_info *sbi = EXFAT_SB(sb); mutex_lock(&sbi->s_lock); - if (test_and_clear_bit(EXFAT_SB_DIRTY, &sbi->s_state)) - sync_blockdev(sb->s_bdev); - exfat_set_vol_flags(sb, VOL_CLEAN); exfat_free_bitmap(sbi); brelse(sbi->boot_bh); mutex_unlock(&sbi->s_lock); @@ -73,13 +70,14 @@ static int exfat_sync_fs(struct super_block *sb, int wait) struct exfat_sb_info *sbi = EXFAT_SB(sb); int err = 0; + if (!wait) + return 0; + /* If there are some dirty buffers in the bdev inode */ mutex_lock(&sbi->s_lock); - if (test_and_clear_bit(EXFAT_SB_DIRTY, &sbi->s_state)) { - sync_blockdev(sb->s_bdev); - if (exfat_set_vol_flags(sb, VOL_CLEAN)) - err = -EIO; - } + sync_blockdev(sb->s_bdev); + if (exfat_set_vol_flags(sb, VOL_CLEAN)) + err = -EIO; mutex_unlock(&sbi->s_lock); return err; } From c5a2c1ed8b71d0272498f94bbd535e87a34c1e82 Mon Sep 17 00:00:00 2001 From: Hyunchul Lee Date: Tue, 16 Jun 2020 15:09:55 +0900 Subject: [PATCH 03/14] exfat: call sync_filesystem for read-only remount We need to commit dirty metadata and pages to disk before remounting exfat as read-only. This fixes a failure in xfstests generic/452 generic/452 does the following: cp something / mount -o remount,ro the /something is corrupted. because while exfat is remounted as read-only, exfat doesn't have a chance to commit metadata and vfs invalidates page caches in a block device. Signed-off-by: Hyunchul Lee Acked-by: Sungjong Seo Signed-off-by: Namjae Jeon --- super.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/super.c b/super.c index 6c1f2e7018e2..401fb3fc1b01 100644 --- a/super.c +++ b/super.c @@ -207,6 +207,18 @@ static void exfat_destroy_inode(struct inode *inode) { call_rcu(&inode->i_rcu, exfat_i_callback); } + +static int exfat_remount(struct super_block *sb, int *flags, char *data) +{ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) + *flags |= SB_NODIRATIME; +#else + *flags |= MS_NODIRATIME; +#endif + + sync_filesystem(sb); + return 0; +} #endif static const struct super_operations exfat_sops = { @@ -215,6 +227,7 @@ static const struct super_operations exfat_sops = { .free_inode = exfat_free_inode, #else .destroy_inode = exfat_destroy_inode, + .remount_fs = exfat_remount, #endif .write_inode = exfat_write_inode, .evict_inode = exfat_evict_inode, @@ -963,10 +976,20 @@ static void exfat_free(struct fs_context *fc) kfree(fc->s_fs_info); } +static int exfat_reconfigure(struct fs_context *fc) +{ + fc->sb_flags |= SB_NODIRATIME; + + /* volume flag will be updated in exfat_sync_fs */ + sync_filesystem(fc->root->d_sb); + return 0; +} + static const struct fs_context_operations exfat_context_ops = { .parse_param = exfat_parse_param, .get_tree = exfat_get_tree, .free = exfat_free, + .reconfigure = exfat_reconfigure, }; static int exfat_init_fs_context(struct fs_context *fc) From 305ebdd9a4a36d9f66716c7d18e2f7ba633fb2ac Mon Sep 17 00:00:00 2001 From: Namjae Jeon Date: Tue, 16 Jun 2020 15:34:22 +0900 Subject: [PATCH 04/14] exfat: enable generic/452 test Signed-off-by: Namjae Jeon --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d1d35e77b6f2..32ef139d6e2d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -183,4 +183,4 @@ script: - sudo ./check generic/448 - sudo ./check generic/450 - sudo ./check generic/451 - # - sudo ./check generic/452 + - sudo ./check generic/452 From e20a3ecca35bf813dcd20b1948e2733e77cc24c4 Mon Sep 17 00:00:00 2001 From: Namjae Jeon Date: Wed, 17 Jun 2020 09:24:53 +0900 Subject: [PATCH 05/14] exfat: move setting VOL_DIRTY over exfat_remove_entries() Move setting VOL_DIRTY over exfat_remove_entries() to avoid unneeded leaving VOL_DIRTY on -ENOTEMPTY. Reported-by: Tetsuhiro Kohada Reviewed-by: Sungjong Seo Signed-off-by: Namjae Jeon --- namei.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/namei.c b/namei.c index aea7a661e0e1..e2d8e0bc65e1 100644 --- a/namei.c +++ b/namei.c @@ -1066,7 +1066,6 @@ static int exfat_rmdir(struct inode *dir, struct dentry *dentry) goto unlock; } - exfat_set_vol_flags(sb, VOL_DIRTY); exfat_chain_set(&clu_to_free, ei->start_clu, EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags); @@ -1093,6 +1092,7 @@ static int exfat_rmdir(struct inode *dir, struct dentry *dentry) num_entries++; brelse(bh); + exfat_set_vol_flags(sb, VOL_DIRTY); err = exfat_remove_entries(dir, &cdir, entry, 0, num_entries); if (err) { exfat_err(sb, "failed to exfat_remove_entries : err(%d)", err); From a29e2ba9702acd3fa54d98bb720e0121290dd887 Mon Sep 17 00:00:00 2001 From: Al Viro Date: Wed, 17 Jun 2020 22:26:10 +0900 Subject: [PATCH 06/14] exfat: fix memory leak in exfat_parse_param() butt3rflyh4ck reported memory leak found by syzkaller. A param->string held by exfat_mount_options. BUG: memory leak unreferenced object 0xffff88801972e090 (size 8): comm "syz-executor.2", pid 16298, jiffies 4295172466 (age 14.060s) hex dump (first 8 bytes): 6b 6f 69 38 2d 75 00 00 koi8-u.. backtrace: [<000000005bfe35d6>] kstrdup+0x36/0x70 mm/util.c:60 [<0000000018ed3277>] exfat_parse_param+0x160/0x5e0 fs/exfat/super.c:276 [<000000007680462b>] vfs_parse_fs_param+0x2b4/0x610 fs/fs_context.c:147 [<0000000097c027f2>] vfs_parse_fs_string+0xe6/0x150 fs/fs_context.c:191 [<00000000371bf78f>] generic_parse_monolithic+0x16f/0x1f0 fs/fs_context.c:231 [<000000005ce5eb1b>] do_new_mount fs/namespace.c:2812 [inline] [<000000005ce5eb1b>] do_mount+0x12bb/0x1b30 fs/namespace.c:3141 [<00000000b642040c>] __do_sys_mount fs/namespace.c:3350 [inline] [<00000000b642040c>] __se_sys_mount fs/namespace.c:3327 [inline] [<00000000b642040c>] __x64_sys_mount+0x18f/0x230 fs/namespace.c:3327 [<000000003b024e98>] do_syscall_64+0xf6/0x7d0 arch/x86/entry/common.c:295 [<00000000ce2b698c>] entry_SYSCALL_64_after_hwframe+0x49/0xb3 exfat_free() should call exfat_free_iocharset(), to prevent a leak in case we fail after parsing iocharset= but before calling get_tree_bdev(). Additionally, there's no point copying param->string in exfat_parse_param() - just steal it, leaving NULL in param->string. That's independent from the leak or fix thereof - it's simply avoiding an extra copy. Fixes: 719c1e182916 ("exfat: add super block operations") Cc: stable@vger.kernel.org # v5.7 Reported-by: butt3rflyh4ck Signed-off-by: Al Viro Signed-off-by: Namjae Jeon --- super.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/super.c b/super.c index 401fb3fc1b01..3993b7e34bb6 100644 --- a/super.c +++ b/super.c @@ -348,9 +348,8 @@ static int exfat_parse_param(struct fs_context *fc, struct fs_parameter *param) break; case Opt_charset: exfat_free_iocharset(sbi); - opts->iocharset = kstrdup(param->string, GFP_KERNEL); - if (!opts->iocharset) - return -ENOMEM; + opts->iocharset = param->string; + param->string = NULL; break; case Opt_errors: opts->errors = result.uint_32; @@ -973,7 +972,12 @@ static int exfat_get_tree(struct fs_context *fc) static void exfat_free(struct fs_context *fc) { - kfree(fc->s_fs_info); + struct exfat_sb_info *sbi = fc->s_fs_info; + + if (sbi) { + exfat_free_iocharset(sbi); + kfree(sbi); + } } static int exfat_reconfigure(struct fs_context *fc) From dc9cda0d125773b6f5f3d1013d1d73a4717a36c1 Mon Sep 17 00:00:00 2001 From: Sungjong Seo Date: Thu, 18 Jun 2020 15:16:40 +0900 Subject: [PATCH 07/14] exfat: flush dirty metadata in fsync generic_file_fsync() exfat used could not guarantee the consistency of a file because it has flushed not dirty metadata but only dirty data pages for a file. Instead of that, use exfat_file_fsync() for files and directories so that it guarantees to commit both the metadata and data pages for a file. Signed-off-by: Sungjong Seo Signed-off-by: Namjae Jeon --- dir.c | 2 +- exfat_fs.h | 1 + file.c | 23 ++++++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/dir.c b/dir.c index 6ef56328e507..603e687591db 100644 --- a/dir.c +++ b/dir.c @@ -310,7 +310,7 @@ const struct file_operations exfat_dir_operations = { .llseek = generic_file_llseek, .read = generic_read_dir, .iterate = exfat_iterate, - .fsync = generic_file_fsync, + .fsync = exfat_file_fsync, }; int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu) diff --git a/exfat_fs.h b/exfat_fs.h index 21653b5d8507..f63c019b3d02 100644 --- a/exfat_fs.h +++ b/exfat_fs.h @@ -425,6 +425,7 @@ int exfat_getattr(const struct path *path, struct kstat *stat, int exfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat); #endif +int exfat_file_fsync(struct file *file, loff_t start, loff_t end, int datasync); /* namei.c */ diff --git a/file.c b/file.c index a55e0b6e99e1..e8f98dc0cf46 100644 --- a/file.c +++ b/file.c @@ -11,6 +11,7 @@ #else #include #endif +#include #include "exfat_raw.h" #include "exfat_fs.h" @@ -381,12 +382,32 @@ out: return error; } +int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) +{ + struct inode *inode = filp->f_mapping->host; + int err; + + err = __generic_file_fsync(filp, start, end, datasync); + if (err) + return err; + + err = sync_blockdev(inode->i_sb->s_bdev); + if (err) + return err; + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) + return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL); +#else + return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL); +#endif +} + const struct file_operations exfat_file_operations = { .llseek = generic_file_llseek, .read_iter = generic_file_read_iter, .write_iter = generic_file_write_iter, .mmap = generic_file_mmap, - .fsync = generic_file_fsync, + .fsync = exfat_file_fsync, .splice_read = generic_file_splice_read, .splice_write = iter_file_splice_write, }; From 773256e4162f050c4557a173edb345059f1a1ff6 Mon Sep 17 00:00:00 2001 From: Tetsuhiro Kohada Date: Wed, 24 Jun 2020 08:32:56 +0900 Subject: [PATCH 08/14] exfat: write multiple sectors at once Write multiple sectors at once when updating dir-entries. Add exfat_update_bhs() for that. It wait for write completion once instead of sector by sector. It's only effective if sync enabled. Signed-off-by: Tetsuhiro Kohada Signed-off-by: Namjae Jeon --- dir.c | 15 +++++++++------ exfat_fs.h | 1 + misc.c | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/dir.c b/dir.c index 603e687591db..d9276d764cee 100644 --- a/dir.c +++ b/dir.c @@ -611,13 +611,16 @@ void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es) void exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync) { - int i; + int i, err = 0; - for (i = 0; i < es->num_bh; i++) { - if (es->modified) - exfat_update_bh(es->bh[i], sync); - brelse(es->bh[i]); - } + if (es->modified) + err = exfat_update_bhs(es->bh, es->num_bh, sync); + + for (i = 0; i < es->num_bh; i++) + if (err) + bforget(es->bh[i]); + else + brelse(es->bh[i]); kfree(es); } diff --git a/exfat_fs.h b/exfat_fs.h index f63c019b3d02..8e86e9251a91 100644 --- a/exfat_fs.h +++ b/exfat_fs.h @@ -522,6 +522,7 @@ void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type); u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type); void exfat_update_bh(struct buffer_head *bh, int sync); +int exfat_update_bhs(struct buffer_head **bhs, int nr_bhs, int sync); void exfat_chain_set(struct exfat_chain *ec, unsigned int dir, unsigned int size, unsigned char flags); void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec); diff --git a/misc.c b/misc.c index 015ac0af9280..1fe22cb55126 100644 --- a/misc.c +++ b/misc.c @@ -178,6 +178,25 @@ void exfat_update_bh(struct buffer_head *bh, int sync) sync_dirty_buffer(bh); } +int exfat_update_bhs(struct buffer_head **bhs, int nr_bhs, int sync) +{ + int i, err = 0; + + for (i = 0; i < nr_bhs; i++) { + set_buffer_uptodate(bhs[i]); + mark_buffer_dirty(bhs[i]); + if (sync) + write_dirty_buffer(bhs[i], 0); + } + + for (i = 0; i < nr_bhs && sync; i++) { + wait_on_buffer(bhs[i]); + if (!err && !buffer_uptodate(bhs[i])) + err = -EIO; + } + return err; +} + void exfat_chain_set(struct exfat_chain *ec, unsigned int dir, unsigned int size, unsigned char flags) { From f9c8dcdd0416e93870758977743198271c520112 Mon Sep 17 00:00:00 2001 From: Tetsuhiro Kohada Date: Wed, 24 Jun 2020 09:41:34 +0900 Subject: [PATCH 09/14] exfat: add error check when updating dir-entries Add error check when synchronously updating dir-entries. Suggested-by: Sungjong Seo Signed-off-by: Tetsuhiro Kohada Signed-off-by: Namjae Jeon --- dir.c | 3 ++- exfat_fs.h | 2 +- file.c | 5 ++++- inode.c | 9 +++++---- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/dir.c b/dir.c index d9276d764cee..fae98c47e572 100644 --- a/dir.c +++ b/dir.c @@ -609,7 +609,7 @@ void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es) es->modified = true; } -void exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync) +int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync) { int i, err = 0; @@ -622,6 +622,7 @@ void exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync) else brelse(es->bh[i]); kfree(es); + return err; } static int exfat_walk_fat_chain(struct super_block *sb, diff --git a/exfat_fs.h b/exfat_fs.h index 8e86e9251a91..0f44bb9e9b51 100644 --- a/exfat_fs.h +++ b/exfat_fs.h @@ -469,7 +469,7 @@ struct exfat_dentry *exfat_get_dentry_cached(struct exfat_entry_set_cache *es, int num); struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb, struct exfat_chain *p_dir, int entry, unsigned int type); -void exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync); +int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync); int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir); /* inode.c */ diff --git a/file.c b/file.c index e8f98dc0cf46..b0a30b34c6c1 100644 --- a/file.c +++ b/file.c @@ -163,6 +163,7 @@ int __exfat_truncate(struct inode *inode, loff_t new_size) struct timespec64 ts; struct exfat_dentry *ep, *ep2; struct exfat_entry_set_cache *es; + int err; es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, ES_ALL_ENTRIES); @@ -201,7 +202,9 @@ int __exfat_truncate(struct inode *inode, loff_t new_size) } exfat_update_dir_chksum_with_entry_set(es); - exfat_free_dentry_set(es, inode_needs_sync(inode)); + err = exfat_free_dentry_set(es, inode_needs_sync(inode)); + if (err) + return err; } /* cut off from the FAT chain */ diff --git a/inode.c b/inode.c index d8beb13731bd..541d634a9304 100644 --- a/inode.c +++ b/inode.c @@ -79,8 +79,7 @@ static int __exfat_write_inode(struct inode *inode, int sync) ep2->dentry.stream.size = ep2->dentry.stream.valid_size; exfat_update_dir_chksum_with_entry_set(es); - exfat_free_dentry_set(es, sync); - return 0; + return exfat_free_dentry_set(es, sync); } int exfat_write_inode(struct inode *inode, struct writeback_control *wbc) @@ -224,6 +223,7 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, if (ei->dir.dir != DIR_DELETED && modified) { struct exfat_dentry *ep; struct exfat_entry_set_cache *es; + int err; es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, ES_ALL_ENTRIES); @@ -242,8 +242,9 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, ep->dentry.stream.valid_size; exfat_update_dir_chksum_with_entry_set(es); - exfat_free_dentry_set(es, inode_needs_sync(inode)); - + err = exfat_free_dentry_set(es, inode_needs_sync(inode)); + if (err) + return err; } /* end of if != DIR_DELETED */ inode->i_blocks += From 3870b2e3dbce2b28876474010f01f471d295ab5b Mon Sep 17 00:00:00 2001 From: Tetsuhiro Kohada Date: Fri, 26 Jun 2020 09:56:20 +0900 Subject: [PATCH 10/14] exfat: optimize exfat_zeroed_cluster() Replace part of exfat_zeroed_cluster() with exfat_update_bhs(). Signed-off-by: Tetsuhiro Kohada Signed-off-by: Namjae Jeon --- fatent.c | 53 ++++++++++------------------------------------------- 1 file changed, 10 insertions(+), 43 deletions(-) diff --git a/fatent.c b/fatent.c index 8de33b8c190f..f2afda61dbb5 100644 --- a/fatent.c +++ b/fatent.c @@ -237,21 +237,6 @@ int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain, return 0; } -static inline int exfat_sync_bhs(struct buffer_head **bhs, int nr_bhs) -{ - int i, err = 0; - - for (i = 0; i < nr_bhs; i++) - write_dirty_buffer(bhs[i], 0); - - for (i = 0; i < nr_bhs; i++) { - wait_on_buffer(bhs[i]); - if (!err && !buffer_uptodate(bhs[i])) - err = -EIO; - } - return err; -} - int exfat_zeroed_cluster(struct inode *dir, unsigned int clu) { struct super_block *sb = dir->i_sb; @@ -273,41 +258,23 @@ int exfat_zeroed_cluster(struct inode *dir, unsigned int clu) } /* Zeroing the unused blocks on this cluster */ - n = 0; while (blknr < last_blknr) { - bhs[n] = sb_getblk(sb, blknr); - if (!bhs[n]) { - err = -ENOMEM; - goto release_bhs; - } - memset(bhs[n]->b_data, 0, sb->s_blocksize); - exfat_update_bh(bhs[n], 0); - - n++; - blknr++; - - if (n == nr_bhs) { - if (IS_DIRSYNC(dir)) { - err = exfat_sync_bhs(bhs, n); - if (err) - goto release_bhs; + for (n = 0; n < nr_bhs && blknr < last_blknr; n++, blknr++) { + bhs[n] = sb_getblk(sb, blknr); + if (!bhs[n]) { + err = -ENOMEM; + goto release_bhs; } - - for (i = 0; i < n; i++) - brelse(bhs[i]); - n = 0; + memset(bhs[n]->b_data, 0, sb->s_blocksize); } - } - if (IS_DIRSYNC(dir)) { - err = exfat_sync_bhs(bhs, n); + err = exfat_update_bhs(bhs, n, IS_DIRSYNC(dir)); if (err) goto release_bhs; + + for (i = 0; i < n; i++) + brelse(bhs[i]); } - - for (i = 0; i < n; i++) - brelse(bhs[i]); - return 0; release_bhs: From 6b09f3cef39c72f55fc86b06dd11c15552331c34 Mon Sep 17 00:00:00 2001 From: Namjae Jeon Date: Wed, 1 Jul 2020 16:43:16 +0900 Subject: [PATCH 11/14] exfat: fix build error on 32bit & 4.19 lower kernel version Signed-off-by: Namjae Jeon --- dir.c | 12 +++++++++--- exfat_fs.h | 18 ++++++++++++++++++ file.c | 4 ++++ misc.c | 18 ++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/dir.c b/dir.c index fae98c47e572..8fdc3f3e416f 100644 --- a/dir.c +++ b/dir.c @@ -441,15 +441,21 @@ int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir, { struct super_block *sb = inode->i_sb; struct exfat_sb_info *sbi = EXFAT_SB(sb); -#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0) - struct timespec64 ts = current_time(inode); +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) + struct timespec64 ts; #else - struct timespec64 ts = CURRENT_TIME_SEC; + struct timespec ts; #endif sector_t sector; struct exfat_dentry *ep; struct buffer_head *bh; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0) + ts = current_time(inode); +#else + ts = CURRENT_TIME_SEC; +#endif + /* * We cannot use exfat_get_dentry_set here because file ep is not * initialized yet. diff --git a/exfat_fs.h b/exfat_fs.h index 0f44bb9e9b51..60650f97673d 100644 --- a/exfat_fs.h +++ b/exfat_fs.h @@ -186,9 +186,15 @@ struct exfat_dir_entry { unsigned short attr; loff_t size; unsigned int num_subdirs; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) struct timespec64 atime; struct timespec64 mtime; struct timespec64 crtime; +#else + struct timespec atime; + struct timespec mtime; + struct timespec crtime; +#endif struct exfat_dentry_namebuf namebuf; }; @@ -296,7 +302,11 @@ struct exfat_inode_info { struct rw_semaphore truncate_lock; struct inode vfs_inode; /* File creation time */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) struct timespec64 i_crtime; +#else + struct timespec i_crtime; +#endif }; static inline struct exfat_sb_info *EXFAT_SB(struct super_block *sb) @@ -514,11 +524,19 @@ void exfat_msg(struct super_block *sb, const char *lv, const char *fmt, ...) #define exfat_info(sb, fmt, ...) \ exfat_msg(sb, KERN_INFO, fmt, ##__VA_ARGS__) +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, u8 tz, __le16 time, __le16 date, u8 time_cs); void exfat_truncate_atime(struct timespec64 *ts); void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, u8 *tz, __le16 *time, __le16 *date, u8 *time_cs); +#else +void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec *ts, + u8 tz, __le16 time, __le16 date, u8 time_cs); +void exfat_truncate_atime(struct timespec *ts); +void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec *ts, + u8 *tz, __le16 *time, __le16 *date, u8 *time_cs); +#endif u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type); u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type); void exfat_update_bh(struct buffer_head *bh, int sync); diff --git a/file.c b/file.c index b0a30b34c6c1..99026432a114 100644 --- a/file.c +++ b/file.c @@ -160,7 +160,11 @@ int __exfat_truncate(struct inode *inode, loff_t new_size) /* update the directory entry */ if (!evict) { +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) struct timespec64 ts; +#else + struct timespec ts; +#endif struct exfat_dentry *ep, *ep2; struct exfat_entry_set_cache *es; int err; diff --git a/misc.c b/misc.c index 1fe22cb55126..62b5957cab47 100644 --- a/misc.c +++ b/misc.c @@ -71,7 +71,11 @@ void exfat_msg(struct super_block *sb, const char *level, const char *fmt, ...) #define SECS_PER_MIN (60) #define TIMEZONE_SEC(x) ((x) * 15 * SECS_PER_MIN) +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) static void exfat_adjust_tz(struct timespec64 *ts, u8 tz_off) +#else +static void exfat_adjust_tz(struct timespec *ts, u8 tz_off) +#endif { if (tz_off <= 0x3F) ts->tv_sec -= TIMEZONE_SEC(tz_off); @@ -80,8 +84,13 @@ static void exfat_adjust_tz(struct timespec64 *ts, u8 tz_off) } /* Convert a EXFAT time/date pair to a UNIX date (seconds since 1 1 70). */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, u8 tz, __le16 time, __le16 date, u8 time_cs) +#else +void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec *ts, + u8 tz, __le16 time, __le16 date, u8 time_cs) +#endif { u16 t = le16_to_cpu(time); u16 d = le16_to_cpu(date); @@ -106,8 +115,13 @@ void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, } /* Convert linear UNIX date to a EXFAT time/date pair. */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, u8 *tz, __le16 *time, __le16 *date, u8 *time_cs) +#else +void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec *ts, + u8 *tz, __le16 *time, __le16 *date, u8 *time_cs) +#endif { struct tm tm; u16 t, d; @@ -136,7 +150,11 @@ void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, } /* atime has only a 2-second resolution */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) void exfat_truncate_atime(struct timespec64 *ts) +#else +void exfat_truncate_atime(struct timespec *ts) +#endif { ts->tv_sec = round_down(ts->tv_sec, 2); ts->tv_nsec = 0; From d7fd859bea58d35efb4392e69a0d7c3cd349b7a2 Mon Sep 17 00:00:00 2001 From: Namjae Jeon Date: Thu, 2 Jul 2020 21:48:01 +0900 Subject: [PATCH 12/14] exfat: fix wrong hint_stat initialization in exfat_find_dir_entry() We found the wrong hint_stat initialization in exfat_find_dir_entry(). It should be initialized when cluster is EXFAT_EOF_CLUSTER. Signed-off-by: Namjae Jeon --- dir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dir.c b/dir.c index 8fdc3f3e416f..2a75286b847f 100644 --- a/dir.c +++ b/dir.c @@ -1127,7 +1127,7 @@ found: ret = exfat_get_next_cluster(sb, &clu.dir); } - if (ret || clu.dir != EXFAT_EOF_CLUSTER) { + if (ret || clu.dir == EXFAT_EOF_CLUSTER) { /* just initialized hint_stat */ hint_stat->clu = p_dir->dir; hint_stat->eidx = 0; From 9cbc7e418ed0b89331b4af54b87197c5109e6f8e Mon Sep 17 00:00:00 2001 From: Namjae Jeon Date: Fri, 3 Jul 2020 08:10:27 +0900 Subject: [PATCH 13/14] exfat: fix overflow issue in exfat_cluster_to_sector() An overflow issue can occur at sector calculation in exfat_cluster_to_sector(). It needs to cast clus's type to sector_t before left shifting. Signed-off-by: Namjae Jeon --- exfat_fs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exfat_fs.h b/exfat_fs.h index 60650f97673d..7cd72fb75ede 100644 --- a/exfat_fs.h +++ b/exfat_fs.h @@ -381,7 +381,7 @@ static inline bool exfat_is_last_sector_in_cluster(struct exfat_sb_info *sbi, static inline sector_t exfat_cluster_to_sector(struct exfat_sb_info *sbi, unsigned int clus) { - return ((clus - EXFAT_RESERVED_CLUSTERS) << sbi->sect_per_clus_bits) + + return ((sector_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->sect_per_clus_bits) + sbi->data_start_sector; } From 44604283a832461009b4c813c500890f55daf776 Mon Sep 17 00:00:00 2001 From: Namjae Jeon Date: Fri, 3 Jul 2020 03:11:11 -0700 Subject: [PATCH 14/14] exfat: release 5.8.4 version - Zero out unused characters of FileName field to avoid a complaint from some fsck tool. - Fix memory leak on error paths. - Fix unnecessary VOL_DIRTY set when calling rmdir on non-empty directory. - Call sync_filesystem() for read-only remount(Fix generic/452 test in xfstests) - Add own fsync() to flush dirty metadata. - fix overflow issue while calculating sector number. Signed-off-by: Namjae Jeon --- exfat_fs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exfat_fs.h b/exfat_fs.h index 7cd72fb75ede..b82e60fff7e3 100644 --- a/exfat_fs.h +++ b/exfat_fs.h @@ -11,7 +11,7 @@ #include #include -#define EXFAT_VERSION "5.8.1" +#define EXFAT_VERSION "5.8.4" #define EXFAT_SUPER_MAGIC 0x2011BAB0UL #define EXFAT_ROOT_INO 1