Skip to content

Commit 306790f

Browse files
carmeli-tamirtorvalds
authored andcommitted
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/[email protected] Signed-off-by: Carmeli Tamir <[email protected]> Acked-by: OGAWA Hirofumi <[email protected]> Reviewed-by: Sergey Senozhatsky <[email protected]> Cc: Johannes Thumshirn <[email protected]> Cc: Bart Van Assche <[email protected]> Cc: Martin K. Petersen <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent d19dc01 commit 306790f

File tree

6 files changed

+39
-22
lines changed

6 files changed

+39
-22
lines changed

fs/fat/cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
363363

364364
*phys = 0;
365365
*mapped_blocks = 0;
366-
if ((sbi->fat_bits != 32) && (inode->i_ino == MSDOS_ROOT_INO)) {
366+
if (!is_fat32(sbi) && (inode->i_ino == MSDOS_ROOT_INO)) {
367367
if (sector < (sbi->dir_entries >> sbi->dir_per_block_bits)) {
368368
*phys = sector + sbi->dir_start;
369369
*mapped_blocks = 1;

fs/fat/dir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
5757
if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
5858
return;
5959
/* root dir of FAT12/FAT16 */
60-
if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
60+
if (!is_fat32(sbi) && (dir->i_ino == MSDOS_ROOT_INO))
6161
return;
6262

6363
bh = sb_find_get_block(sb, phys);
@@ -1313,7 +1313,7 @@ int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
13131313
}
13141314
}
13151315
if (dir->i_ino == MSDOS_ROOT_INO) {
1316-
if (sbi->fat_bits != 32)
1316+
if (!is_fat32(sbi))
13171317
goto error;
13181318
} else if (MSDOS_I(dir)->i_start == 0) {
13191319
fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",

fs/fat/fat.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,32 @@ static inline struct msdos_sb_info *MSDOS_SB(struct super_block *sb)
142142
return sb->s_fs_info;
143143
}
144144

145+
/*
146+
* Functions that determine the variant of the FAT file system (i.e.,
147+
* whether this is FAT12, FAT16 or FAT32.
148+
*/
149+
static inline bool is_fat12(const struct msdos_sb_info *sbi)
150+
{
151+
return sbi->fat_bits == 12;
152+
}
153+
154+
static inline bool is_fat16(const struct msdos_sb_info *sbi)
155+
{
156+
return sbi->fat_bits == 16;
157+
}
158+
159+
static inline bool is_fat32(const struct msdos_sb_info *sbi)
160+
{
161+
return sbi->fat_bits == 32;
162+
}
163+
145164
/* Maximum number of clusters */
146165
static inline u32 max_fat(struct super_block *sb)
147166
{
148167
struct msdos_sb_info *sbi = MSDOS_SB(sb);
149168

150-
return sbi->fat_bits == 32 ? MAX_FAT32 :
151-
sbi->fat_bits == 16 ? MAX_FAT16 : MAX_FAT12;
169+
return is_fat32(sbi) ? MAX_FAT32 :
170+
is_fat16(sbi) ? MAX_FAT16 : MAX_FAT12;
152171
}
153172

154173
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,
266285
const struct msdos_dir_entry *de)
267286
{
268287
int cluster = le16_to_cpu(de->start);
269-
if (sbi->fat_bits == 32)
288+
if (is_fat32(sbi))
270289
cluster |= (le16_to_cpu(de->starthi) << 16);
271290
return cluster;
272291
}

fs/fat/fatent.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,27 +290,25 @@ void fat_ent_access_init(struct super_block *sb)
290290

291291
mutex_init(&sbi->fat_lock);
292292

293-
switch (sbi->fat_bits) {
294-
case 32:
293+
if (is_fat32(sbi)) {
295294
sbi->fatent_shift = 2;
296295
sbi->fatent_ops = &fat32_ops;
297-
break;
298-
case 16:
296+
} else if (is_fat16(sbi)) {
299297
sbi->fatent_shift = 1;
300298
sbi->fatent_ops = &fat16_ops;
301-
break;
302-
case 12:
299+
} else if (is_fat12(sbi)) {
303300
sbi->fatent_shift = -1;
304301
sbi->fatent_ops = &fat12_ops;
305-
break;
302+
} else {
303+
fat_fs_error(sb, "invalid FAT variant, %u bits", sbi->fat_bits);
306304
}
307305
}
308306

309307
static void mark_fsinfo_dirty(struct super_block *sb)
310308
{
311309
struct msdos_sb_info *sbi = MSDOS_SB(sb);
312310

313-
if (sb_rdonly(sb) || sbi->fat_bits != 32)
311+
if (sb_rdonly(sb) || !is_fat32(sbi))
314312
return;
315313

316314
__mark_inode_dirty(sbi->fsinfo_inode, I_DIRTY_SYNC);
@@ -327,7 +325,7 @@ static inline int fat_ent_update_ptr(struct super_block *sb,
327325
/* Is this fatent's blocks including this entry? */
328326
if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr)
329327
return 0;
330-
if (sbi->fat_bits == 12) {
328+
if (is_fat12(sbi)) {
331329
if ((offset + 1) < sb->s_blocksize) {
332330
/* This entry is on bhs[0]. */
333331
if (fatent->nr_bhs == 2) {

fs/fat/inode.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ static void fat_set_state(struct super_block *sb,
686686

687687
b = (struct fat_boot_sector *) bh->b_data;
688688

689-
if (sbi->fat_bits == 32) {
689+
if (is_fat32(sbi)) {
690690
if (set)
691691
b->fat32.state |= FAT_STATE_DIRTY;
692692
else
@@ -1396,7 +1396,7 @@ static int fat_read_root(struct inode *inode)
13961396
inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
13971397
inode->i_op = sbi->dir_ops;
13981398
inode->i_fop = &fat_dir_operations;
1399-
if (sbi->fat_bits == 32) {
1399+
if (is_fat32(sbi)) {
14001400
MSDOS_I(inode)->i_start = sbi->root_cluster;
14011401
error = fat_calc_dir_size(inode);
14021402
if (error < 0)
@@ -1423,7 +1423,7 @@ static unsigned long calc_fat_clusters(struct super_block *sb)
14231423
struct msdos_sb_info *sbi = MSDOS_SB(sb);
14241424

14251425
/* Divide first to avoid overflow */
1426-
if (sbi->fat_bits != 12) {
1426+
if (!is_fat12(sbi)) {
14271427
unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
14281428
return ent_per_sec * sbi->fat_length;
14291429
}
@@ -1743,7 +1743,7 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
17431743
}
17441744

17451745
/* interpret volume ID as a little endian 32 bit integer */
1746-
if (sbi->fat_bits == 32)
1746+
if (is_fat32(sbi))
17471747
sbi->vol_id = bpb.fat32_vol_id;
17481748
else /* fat 16 or 12 */
17491749
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,
17691769

17701770
total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
17711771

1772-
if (sbi->fat_bits != 32)
1772+
if (!is_fat32(sbi))
17731773
sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
17741774

17751775
/* some OSes set FAT_STATE_DIRTY and clean it on unmount. */
1776-
if (sbi->fat_bits == 32)
1776+
if (is_fat32(sbi))
17771777
sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY;
17781778
else /* fat 16 or 12 */
17791779
sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY;

fs/fat/misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int fat_clusters_flush(struct super_block *sb)
6464
struct buffer_head *bh;
6565
struct fat_boot_fsinfo *fsinfo;
6666

67-
if (sbi->fat_bits != 32)
67+
if (!is_fat32(sbi))
6868
return 0;
6969

7070
bh = sb_bread(sb, sbi->fsinfo_sector);

0 commit comments

Comments
 (0)