Skip to content

Commit e40e023

Browse files
committed
Merge tag 'exfat-for-6.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/exfat
Pull exfat updates from Namjae Jeon: - Fix a remount failure caused by differing process masks by inheriting the original mount options during the remount process - Fix a potential divide-by-zero error and system crash in exfat_allocate_bitmap that occurred when the readahead count was zero - Add validation for directory cluster bitmap bits to prevent directory and root cluster from being incorrectly zeroed out on corrupted images - Clear the post-EOF page cache when extending a file to prevent stale mmap data from becoming visible, addressing an generic/363 failure - Fix a reference count leak in exfat_find by properly releasing the dentry set in specific error paths * tag 'exfat-for-6.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/exfat: exfat: fix remount failure in different process environments exfat: fix divide-by-zero in exfat_allocate_bitmap exfat: validate the cluster bitmap bits of directory exfat: zero out post-EOF page cache on file extension exfat: fix refcount leak in exfat_find
2 parents 4b6b432 + 51fc7b4 commit e40e023

7 files changed

Lines changed: 77 additions & 24 deletions

File tree

fs/exfat/balloc.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static int exfat_allocate_bitmap(struct super_block *sb,
106106
(PAGE_SHIFT - sb->s_blocksize_bits);
107107
for (i = 0; i < sbi->map_sectors; i++) {
108108
/* Trigger the next readahead in advance. */
109-
if (0 == (i % max_ra_count)) {
109+
if (max_ra_count && 0 == (i % max_ra_count)) {
110110
blk_start_plug(&plug);
111111
for (j = i; j < min(max_ra_count, sbi->map_sectors - i) + i; j++)
112112
sb_breadahead(sb, sector + j);
@@ -183,11 +183,10 @@ void exfat_free_bitmap(struct exfat_sb_info *sbi)
183183
kvfree(sbi->vol_amap);
184184
}
185185

186-
int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync)
186+
int exfat_set_bitmap(struct super_block *sb, unsigned int clu, bool sync)
187187
{
188188
int i, b;
189189
unsigned int ent_idx;
190-
struct super_block *sb = inode->i_sb;
191190
struct exfat_sb_info *sbi = EXFAT_SB(sb);
192191

193192
if (!is_valid_cluster(sbi, clu))
@@ -202,11 +201,10 @@ int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync)
202201
return 0;
203202
}
204203

205-
int exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync)
204+
int exfat_clear_bitmap(struct super_block *sb, unsigned int clu, bool sync)
206205
{
207206
int i, b;
208207
unsigned int ent_idx;
209-
struct super_block *sb = inode->i_sb;
210208
struct exfat_sb_info *sbi = EXFAT_SB(sb);
211209

212210
if (!is_valid_cluster(sbi, clu))
@@ -226,6 +224,28 @@ int exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync)
226224
return 0;
227225
}
228226

227+
bool exfat_test_bitmap(struct super_block *sb, unsigned int clu)
228+
{
229+
int i, b;
230+
unsigned int ent_idx;
231+
struct exfat_sb_info *sbi = EXFAT_SB(sb);
232+
233+
if (!sbi->vol_amap)
234+
return true;
235+
236+
if (!is_valid_cluster(sbi, clu))
237+
return false;
238+
239+
ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
240+
i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
241+
b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
242+
243+
if (!test_bit_le(b, sbi->vol_amap[i]->b_data))
244+
return false;
245+
246+
return true;
247+
}
248+
229249
/*
230250
* If the value of "clu" is 0, it means cluster 2 which is the first cluster of
231251
* the cluster heap.

fs/exfat/dir.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,11 @@ static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir
604604
if (ret)
605605
return ret;
606606

607+
if (!exfat_test_bitmap(sb, clu)) {
608+
exfat_err(sb, "failed to test cluster bit(%u)", clu);
609+
return -EIO;
610+
}
611+
607612
/* byte offset in cluster */
608613
off = EXFAT_CLU_OFFSET(off, sbi);
609614

fs/exfat/exfat_fs.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,9 @@ int exfat_count_num_clusters(struct super_block *sb,
452452
/* balloc.c */
453453
int exfat_load_bitmap(struct super_block *sb);
454454
void exfat_free_bitmap(struct exfat_sb_info *sbi);
455-
int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync);
456-
int exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync);
455+
int exfat_set_bitmap(struct super_block *sb, unsigned int clu, bool sync);
456+
int exfat_clear_bitmap(struct super_block *sb, unsigned int clu, bool sync);
457+
bool exfat_test_bitmap(struct super_block *sb, unsigned int clu);
457458
unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu);
458459
int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count);
459460
int exfat_trim_fs(struct inode *inode, struct fstrim_range *range);

fs/exfat/fatent.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static int __exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain
205205
cur_cmap_i = next_cmap_i;
206206
}
207207

208-
err = exfat_clear_bitmap(inode, clu, (sync && IS_DIRSYNC(inode)));
208+
err = exfat_clear_bitmap(sb, clu, (sync && IS_DIRSYNC(inode)));
209209
if (err)
210210
break;
211211
clu++;
@@ -233,7 +233,7 @@ static int __exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain
233233
cur_cmap_i = next_cmap_i;
234234
}
235235

236-
if (exfat_clear_bitmap(inode, clu, (sync && IS_DIRSYNC(inode))))
236+
if (exfat_clear_bitmap(sb, clu, (sync && IS_DIRSYNC(inode))))
237237
break;
238238

239239
if (sbi->options.discard) {
@@ -409,7 +409,7 @@ int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
409409
}
410410

411411
/* update allocation bitmap */
412-
if (exfat_set_bitmap(inode, new_clu, sync_bmap)) {
412+
if (exfat_set_bitmap(sb, new_clu, sync_bmap)) {
413413
ret = -EIO;
414414
goto free_cluster;
415415
}

fs/exfat/file.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ static int exfat_cont_expand(struct inode *inode, loff_t size)
2525
struct exfat_sb_info *sbi = EXFAT_SB(sb);
2626
struct exfat_chain clu;
2727

28+
truncate_pagecache(inode, i_size_read(inode));
29+
2830
ret = inode_newsize_ok(inode, size);
2931
if (ret)
3032
return ret;
@@ -639,6 +641,9 @@ static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
639641

640642
inode_lock(inode);
641643

644+
if (pos > i_size_read(inode))
645+
truncate_pagecache(inode, i_size_read(inode));
646+
642647
valid_size = ei->valid_size;
643648

644649
ret = generic_write_checks(iocb, iter);

fs/exfat/namei.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -645,16 +645,6 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
645645
info->valid_size = le64_to_cpu(ep2->dentry.stream.valid_size);
646646
info->size = le64_to_cpu(ep2->dentry.stream.size);
647647

648-
if (info->valid_size < 0) {
649-
exfat_fs_error(sb, "data valid size is invalid(%lld)", info->valid_size);
650-
return -EIO;
651-
}
652-
653-
if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) {
654-
exfat_fs_error(sb, "data size is invalid(%lld)", info->size);
655-
return -EIO;
656-
}
657-
658648
info->start_clu = le32_to_cpu(ep2->dentry.stream.start_clu);
659649
if (!is_valid_cluster(sbi, info->start_clu) && info->size) {
660650
exfat_warn(sb, "start_clu is invalid cluster(0x%x)",
@@ -692,6 +682,16 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
692682
0);
693683
exfat_put_dentry_set(&es, false);
694684

685+
if (info->valid_size < 0) {
686+
exfat_fs_error(sb, "data valid size is invalid(%lld)", info->valid_size);
687+
return -EIO;
688+
}
689+
690+
if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) {
691+
exfat_fs_error(sb, "data size is invalid(%lld)", info->size);
692+
return -EIO;
693+
}
694+
695695
if (ei->start_clu == EXFAT_FREE_CLUSTER) {
696696
exfat_fs_error(sb,
697697
"non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",

fs/exfat/super.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,17 @@ static int __exfat_fill_super(struct super_block *sb,
629629
goto free_bh;
630630
}
631631

632+
if (!exfat_test_bitmap(sb, sbi->root_dir)) {
633+
exfat_warn(sb, "failed to test first cluster bit of root dir(%u)",
634+
sbi->root_dir);
635+
/*
636+
* The first cluster bit of the root directory should never
637+
* be unset except when storage is corrupted. This bit is
638+
* set to allow operations after mount.
639+
*/
640+
exfat_set_bitmap(sb, sbi->root_dir, false);
641+
}
642+
632643
ret = exfat_count_used_clusters(sb, &sbi->used_clusters);
633644
if (ret) {
634645
exfat_err(sb, "failed to scan clusters");
@@ -813,10 +824,21 @@ static int exfat_init_fs_context(struct fs_context *fc)
813824
ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
814825
DEFAULT_RATELIMIT_BURST);
815826

816-
sbi->options.fs_uid = current_uid();
817-
sbi->options.fs_gid = current_gid();
818-
sbi->options.fs_fmask = current->fs->umask;
819-
sbi->options.fs_dmask = current->fs->umask;
827+
if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE && fc->root) {
828+
struct super_block *sb = fc->root->d_sb;
829+
struct exfat_mount_options *cur_opts = &EXFAT_SB(sb)->options;
830+
831+
sbi->options.fs_uid = cur_opts->fs_uid;
832+
sbi->options.fs_gid = cur_opts->fs_gid;
833+
sbi->options.fs_fmask = cur_opts->fs_fmask;
834+
sbi->options.fs_dmask = cur_opts->fs_dmask;
835+
} else {
836+
sbi->options.fs_uid = current_uid();
837+
sbi->options.fs_gid = current_gid();
838+
sbi->options.fs_fmask = current->fs->umask;
839+
sbi->options.fs_dmask = current->fs->umask;
840+
}
841+
820842
sbi->options.allow_utime = -1;
821843
sbi->options.errors = EXFAT_ERRORS_RO;
822844
exfat_set_iocharset(&sbi->options, exfat_default_iocharset);

0 commit comments

Comments
 (0)