fat: new inline functions to determine the FAT variant (32, 16 or 12)

This patch introduces 3 new inline functions - is_fat12, is_fat16 and
is_fat32, and replaces every occurrence in the code in which the FS
variant (whether this is FAT12, FAT16 or FAT32) was previously checked
using msdos_sb_info->fat_bits.

Link: http://lkml.kernel.org/r/1544990640-11604-4-git-send-email-carmeli.tamir@gmail.com
Signed-off-by: Carmeli Tamir <carmeli.tamir@gmail.com>
Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Cc: Johannes Thumshirn <jthumshirn@suse.de>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Carmeli Tamir 2019-01-03 15:28:00 -08:00 committed by Linus Torvalds
parent d19dc01618
commit 306790f75a
6 changed files with 39 additions and 22 deletions

View File

@ -363,7 +363,7 @@ int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
*phys = 0; *phys = 0;
*mapped_blocks = 0; *mapped_blocks = 0;
if ((sbi->fat_bits != 32) && (inode->i_ino == MSDOS_ROOT_INO)) { if (!is_fat32(sbi) && (inode->i_ino == MSDOS_ROOT_INO)) {
if (sector < (sbi->dir_entries >> sbi->dir_per_block_bits)) { if (sector < (sbi->dir_entries >> sbi->dir_per_block_bits)) {
*phys = sector + sbi->dir_start; *phys = sector + sbi->dir_start;
*mapped_blocks = 1; *mapped_blocks = 1;

View File

@ -57,7 +57,7 @@ static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1) if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
return; return;
/* root dir of FAT12/FAT16 */ /* root dir of FAT12/FAT16 */
if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO)) if (!is_fat32(sbi) && (dir->i_ino == MSDOS_ROOT_INO))
return; return;
bh = sb_find_get_block(sb, phys); bh = sb_find_get_block(sb, phys);
@ -1313,7 +1313,7 @@ int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
} }
} }
if (dir->i_ino == MSDOS_ROOT_INO) { if (dir->i_ino == MSDOS_ROOT_INO) {
if (sbi->fat_bits != 32) if (!is_fat32(sbi))
goto error; goto error;
} else if (MSDOS_I(dir)->i_start == 0) { } else if (MSDOS_I(dir)->i_start == 0) {
fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)", fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",

View File

@ -142,13 +142,32 @@ static inline struct msdos_sb_info *MSDOS_SB(struct super_block *sb)
return sb->s_fs_info; return sb->s_fs_info;
} }
/*
* Functions that determine the variant of the FAT file system (i.e.,
* whether this is FAT12, FAT16 or FAT32.
*/
static inline bool is_fat12(const struct msdos_sb_info *sbi)
{
return sbi->fat_bits == 12;
}
static inline bool is_fat16(const struct msdos_sb_info *sbi)
{
return sbi->fat_bits == 16;
}
static inline bool is_fat32(const struct msdos_sb_info *sbi)
{
return sbi->fat_bits == 32;
}
/* Maximum number of clusters */ /* Maximum number of clusters */
static inline u32 max_fat(struct super_block *sb) static inline u32 max_fat(struct super_block *sb)
{ {
struct msdos_sb_info *sbi = MSDOS_SB(sb); struct msdos_sb_info *sbi = MSDOS_SB(sb);
return sbi->fat_bits == 32 ? MAX_FAT32 : return is_fat32(sbi) ? MAX_FAT32 :
sbi->fat_bits == 16 ? MAX_FAT16 : MAX_FAT12; is_fat16(sbi) ? MAX_FAT16 : MAX_FAT12;
} }
static inline struct msdos_inode_info *MSDOS_I(struct inode *inode) static inline struct msdos_inode_info *MSDOS_I(struct inode *inode)
@ -266,7 +285,7 @@ static inline int fat_get_start(const struct msdos_sb_info *sbi,
const struct msdos_dir_entry *de) const struct msdos_dir_entry *de)
{ {
int cluster = le16_to_cpu(de->start); int cluster = le16_to_cpu(de->start);
if (sbi->fat_bits == 32) if (is_fat32(sbi))
cluster |= (le16_to_cpu(de->starthi) << 16); cluster |= (le16_to_cpu(de->starthi) << 16);
return cluster; return cluster;
} }

View File

@ -290,19 +290,17 @@ void fat_ent_access_init(struct super_block *sb)
mutex_init(&sbi->fat_lock); mutex_init(&sbi->fat_lock);
switch (sbi->fat_bits) { if (is_fat32(sbi)) {
case 32:
sbi->fatent_shift = 2; sbi->fatent_shift = 2;
sbi->fatent_ops = &fat32_ops; sbi->fatent_ops = &fat32_ops;
break; } else if (is_fat16(sbi)) {
case 16:
sbi->fatent_shift = 1; sbi->fatent_shift = 1;
sbi->fatent_ops = &fat16_ops; sbi->fatent_ops = &fat16_ops;
break; } else if (is_fat12(sbi)) {
case 12:
sbi->fatent_shift = -1; sbi->fatent_shift = -1;
sbi->fatent_ops = &fat12_ops; sbi->fatent_ops = &fat12_ops;
break; } else {
fat_fs_error(sb, "invalid FAT variant, %u bits", sbi->fat_bits);
} }
} }
@ -310,7 +308,7 @@ static void mark_fsinfo_dirty(struct super_block *sb)
{ {
struct msdos_sb_info *sbi = MSDOS_SB(sb); struct msdos_sb_info *sbi = MSDOS_SB(sb);
if (sb_rdonly(sb) || sbi->fat_bits != 32) if (sb_rdonly(sb) || !is_fat32(sbi))
return; return;
__mark_inode_dirty(sbi->fsinfo_inode, I_DIRTY_SYNC); __mark_inode_dirty(sbi->fsinfo_inode, I_DIRTY_SYNC);
@ -327,7 +325,7 @@ static inline int fat_ent_update_ptr(struct super_block *sb,
/* Is this fatent's blocks including this entry? */ /* Is this fatent's blocks including this entry? */
if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr) if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr)
return 0; return 0;
if (sbi->fat_bits == 12) { if (is_fat12(sbi)) {
if ((offset + 1) < sb->s_blocksize) { if ((offset + 1) < sb->s_blocksize) {
/* This entry is on bhs[0]. */ /* This entry is on bhs[0]. */
if (fatent->nr_bhs == 2) { if (fatent->nr_bhs == 2) {

View File

@ -686,7 +686,7 @@ static void fat_set_state(struct super_block *sb,
b = (struct fat_boot_sector *) bh->b_data; b = (struct fat_boot_sector *) bh->b_data;
if (sbi->fat_bits == 32) { if (is_fat32(sbi)) {
if (set) if (set)
b->fat32.state |= FAT_STATE_DIRTY; b->fat32.state |= FAT_STATE_DIRTY;
else else
@ -1396,7 +1396,7 @@ static int fat_read_root(struct inode *inode)
inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO); inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
inode->i_op = sbi->dir_ops; inode->i_op = sbi->dir_ops;
inode->i_fop = &fat_dir_operations; inode->i_fop = &fat_dir_operations;
if (sbi->fat_bits == 32) { if (is_fat32(sbi)) {
MSDOS_I(inode)->i_start = sbi->root_cluster; MSDOS_I(inode)->i_start = sbi->root_cluster;
error = fat_calc_dir_size(inode); error = fat_calc_dir_size(inode);
if (error < 0) if (error < 0)
@ -1423,7 +1423,7 @@ static unsigned long calc_fat_clusters(struct super_block *sb)
struct msdos_sb_info *sbi = MSDOS_SB(sb); struct msdos_sb_info *sbi = MSDOS_SB(sb);
/* Divide first to avoid overflow */ /* Divide first to avoid overflow */
if (sbi->fat_bits != 12) { if (!is_fat12(sbi)) {
unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits; unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
return ent_per_sec * sbi->fat_length; return ent_per_sec * sbi->fat_length;
} }
@ -1743,7 +1743,7 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
} }
/* interpret volume ID as a little endian 32 bit integer */ /* interpret volume ID as a little endian 32 bit integer */
if (sbi->fat_bits == 32) if (is_fat32(sbi))
sbi->vol_id = bpb.fat32_vol_id; sbi->vol_id = bpb.fat32_vol_id;
else /* fat 16 or 12 */ else /* fat 16 or 12 */
sbi->vol_id = bpb.fat16_vol_id; sbi->vol_id = bpb.fat16_vol_id;
@ -1769,11 +1769,11 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus; total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
if (sbi->fat_bits != 32) if (!is_fat32(sbi))
sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12; sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
/* some OSes set FAT_STATE_DIRTY and clean it on unmount. */ /* some OSes set FAT_STATE_DIRTY and clean it on unmount. */
if (sbi->fat_bits == 32) if (is_fat32(sbi))
sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY; sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY;
else /* fat 16 or 12 */ else /* fat 16 or 12 */
sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY; sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY;

View File

@ -64,7 +64,7 @@ int fat_clusters_flush(struct super_block *sb)
struct buffer_head *bh; struct buffer_head *bh;
struct fat_boot_fsinfo *fsinfo; struct fat_boot_fsinfo *fsinfo;
if (sbi->fat_bits != 32) if (!is_fat32(sbi))
return 0; return 0;
bh = sb_bread(sb, sbi->fsinfo_sector); bh = sb_bread(sb, sbi->fsinfo_sector);