- Replaces the "number of reserved clusters" by FAT_START_ENT.
- The ->clusters is total number of clusters. Instead of it, use the
maximum cluster number (->max_cluster).
By this change, removes the some "->clusters + 2" calculation.
- Adds inline function for cluster to block number conversion. And
replaces the open-coding for it.
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
int next;
next = -EIO;
- if (nr < 2 || MSDOS_SB(sb)->clusters + 2 <= nr) {
+ if (nr < FAT_START_ENT || MSDOS_SB(sb)->max_cluster <= nr) {
fat_fs_panic(sb, "invalid access to FAT (entry 0x%08x)", nr);
goto out;
}
cluster = fat_bmap_cluster(inode, cluster);
if (cluster < 0)
return cluster;
- else if (cluster) {
- *phys = ((sector_t)cluster - 2) * sbi->sec_per_clus
- + sbi->data_start + offset;
- }
+ else if (cluster)
+ *phys = fat_clus_to_blknr(sbi, cluster) + offset;
return 0;
}
if (nr < 0)
return ERR_PTR(nr);
- sector = ((sector_t)nr - 2) * sec_per_clus + MSDOS_SB(sb)->data_start;
+ sector = fat_clus_to_blknr(MSDOS_SB(sb), nr);
last_sector = sector + sec_per_clus;
for ( ; sector < last_sector; sector++) {
if ((bh = sb_getblk(sb, sector))) {
static int fat_statfs(struct super_block *sb, struct kstatfs *buf)
{
+ struct msdos_sb_info *sbi = MSDOS_SB(sb);
int free, nr, ret;
- if (MSDOS_SB(sb)->free_clusters != -1)
- free = MSDOS_SB(sb)->free_clusters;
+ if (sbi->free_clusters != -1)
+ free = sbi->free_clusters;
else {
lock_fat(sb);
- if (MSDOS_SB(sb)->free_clusters != -1)
- free = MSDOS_SB(sb)->free_clusters;
+ if (sbi->free_clusters != -1)
+ free = sbi->free_clusters;
else {
free = 0;
- for (nr = 2; nr < MSDOS_SB(sb)->clusters + 2; nr++) {
+ for (nr = FAT_START_ENT; nr < sbi->max_cluster; nr++) {
ret = fat_access(sb, nr, -1);
if (ret < 0) {
unlock_fat(sb);
} else if (ret == FAT_ENT_FREE)
free++;
}
- MSDOS_SB(sb)->free_clusters = free;
+ sbi->free_clusters = free;
}
unlock_fat(sb);
}
buf->f_type = sb->s_magic;
- buf->f_bsize = MSDOS_SB(sb)->cluster_size;
- buf->f_blocks = MSDOS_SB(sb)->clusters;
+ buf->f_bsize = sbi->cluster_size;
+ buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
buf->f_bfree = free;
buf->f_bavail = free;
- buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
+ buf->f_namelen = sbi->options.isvfat ? 260 : 12;
return 0;
}
/* check that FAT table does not overflow */
fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
- total_clusters = min(total_clusters, fat_clusters - 2);
+ total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
if (total_clusters > MAX_FAT(sb)) {
if (!silent)
printk(KERN_ERR "FAT: count of clusters too big (%u)\n",
goto out_invalid;
}
- sbi->clusters = total_clusters;
+ sbi->max_cluster = total_clusters + FAT_START_ENT;
/* check the free_clusters, it's not necessarily correct */
- if (sbi->free_clusters != -1 && sbi->free_clusters > sbi->clusters)
+ if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
sbi->free_clusters = -1;
brelse(bh);
int fat_add_cluster(struct inode *inode)
{
struct super_block *sb = inode->i_sb;
+ struct msdos_sb_info *sbi = MSDOS_SB(sb);
int ret, count, limit, new_dclus, new_fclus, last;
- int cluster_bits = MSDOS_SB(sb)->cluster_bits;
+ int cluster_bits = sbi->cluster_bits;
/*
* We must locate the last cluster of the file to add this new
/* find free FAT entry */
lock_fat(sb);
- if (MSDOS_SB(sb)->free_clusters == 0) {
+ if (sbi->free_clusters == 0) {
unlock_fat(sb);
return -ENOSPC;
}
- limit = MSDOS_SB(sb)->clusters + 2;
- new_dclus = MSDOS_SB(sb)->prev_free + 1;
- for (count = 0; count < MSDOS_SB(sb)->clusters; count++, new_dclus++) {
+ limit = sbi->max_cluster;
+ new_dclus = sbi->prev_free + 1;
+ for (count = FAT_START_ENT; count < limit; count++, new_dclus++) {
new_dclus = new_dclus % limit;
- if (new_dclus < 2)
- new_dclus = 2;
+ if (new_dclus < FAT_START_ENT)
+ new_dclus = FAT_START_ENT;
ret = fat_access(sb, new_dclus, -1);
if (ret < 0) {
} else if (ret == FAT_ENT_FREE)
break;
}
- if (count >= MSDOS_SB(sb)->clusters) {
- MSDOS_SB(sb)->free_clusters = 0;
+ if (count >= limit) {
+ sbi->free_clusters = 0;
unlock_fat(sb);
return -ENOSPC;
}
return ret;
}
- MSDOS_SB(sb)->prev_free = new_dclus;
- if (MSDOS_SB(sb)->free_clusters != -1)
- MSDOS_SB(sb)->free_clusters--;
+ sbi->prev_free = new_dclus;
+ if (sbi->free_clusters != -1)
+ sbi->free_clusters--;
fat_clusters_flush(sb);
unlock_fat(sb);
new_fclus, inode->i_blocks >> (cluster_bits - 9));
fat_cache_inval_inode(inode);
}
- inode->i_blocks += MSDOS_SB(sb)->cluster_size >> 9;
+ inode->i_blocks += sbi->cluster_size >> 9;
return new_dclus;
}
#define FAT_FIRST_ENT(s, x) ((MSDOS_SB(s)->fat_bits == 32 ? 0x0FFFFF00 : \
MSDOS_SB(s)->fat_bits == 16 ? 0xFF00 : 0xF00) | (x))
+/* start of data cluster's entry (number of reserved clusters) */
+#define FAT_START_ENT 2
+
/* maximum number of clusters */
#define MAX_FAT12 0xFF4
#define MAX_FAT16 0xFFF4
unsigned long dir_start;
unsigned short dir_entries; /* root dir start & entries */
unsigned long data_start; /* first data sector */
- unsigned long clusters; /* number of clusters */
+ unsigned long max_cluster; /* maximum cluster number */
unsigned long root_cluster; /* first cluster of the root directory */
unsigned long fsinfo_sector; /* sector number of FAT32 fsinfo */
struct semaphore fat_lock;
return container_of(inode, struct msdos_inode_info, vfs_inode);
}
+static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus)
+{
+ return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus
+ + sbi->data_start;
+}
+
static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len)
{
#ifdef __BIG_ENDIAN