Commit b438aca4 authored by Hirofumi Ogawa's avatar Hirofumi Ogawa Committed by Arnaldo Carvalho de Melo

[PATCH] FAT cluster chain cache per superblock (4/5)

This shifts the data position caches from module to per-superblock, and
cleanups.
parent ff3345fd
......@@ -12,9 +12,6 @@
#include <linux/msdos_fs.h>
#include <linux/buffer_head.h>
static struct fat_cache *fat_cache,cache[FAT_CACHE];
static spinlock_t fat_cache_lock = SPIN_LOCK_UNLOCKED;
int __fat_access(struct super_block *sb, int nr, int new_value)
{
struct msdos_sb_info *sbi = MSDOS_SB(sb);
......@@ -133,148 +130,139 @@ int fat_access(struct super_block *sb, int nr, int new_value)
return next;
}
void fat_cache_init(void)
void fat_cache_init(struct super_block *sb)
{
static int initialized;
struct msdos_sb_info *sbi = MSDOS_SB(sb);
int count;
spin_lock(&fat_cache_lock);
if (initialized) {
spin_unlock(&fat_cache_lock);
return;
}
fat_cache = &cache[0];
for (count = 0; count < FAT_CACHE; count++) {
cache[count].sb = NULL;
cache[count].next = count == FAT_CACHE-1 ? NULL :
&cache[count+1];
spin_lock_init(&sbi->cache_lock);
for (count = 0; count < FAT_CACHE_NR - 1; count++) {
sbi->cache_array[count].start_cluster = 0;
sbi->cache_array[count].next = &sbi->cache_array[count + 1];
}
initialized = 1;
spin_unlock(&fat_cache_lock);
sbi->cache_array[count].start_cluster = 0;
sbi->cache_array[count].next = NULL;
sbi->cache = sbi->cache_array;
}
void fat_cache_lookup(struct inode *inode,int cluster,int *f_clu,int *d_clu)
void fat_cache_lookup(struct inode *inode, int cluster, int *f_clu, int *d_clu)
{
struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
struct fat_cache *walk;
int first = MSDOS_I(inode)->i_start;
int first;
first = MSDOS_I(inode)->i_start;
if (!first)
return;
spin_lock(&fat_cache_lock);
for (walk = fat_cache; walk; walk = walk->next)
if (inode->i_sb == walk->sb
&& walk->start_cluster == first
spin_lock(&sbi->cache_lock);
for (walk = sbi->cache; walk; walk = walk->next) {
if (walk->start_cluster == first
&& walk->file_cluster <= cluster
&& walk->file_cluster > *f_clu) {
*d_clu = walk->disk_cluster;
*f_clu = walk->file_cluster;
#ifdef DEBUG
printk("cache hit: %d (%d)\n",walk->file_cluster,*d_clu);
printk("cache hit: %d (%d)\n", *f_clu, *d_clu);
#endif
if ((*f_clu = walk->file_cluster) == cluster) {
spin_unlock(&fat_cache_lock);
return;
}
if (*f_clu == cluster)
goto out;
}
spin_unlock(&fat_cache_lock);
}
#ifdef DEBUG
printk("cache miss\n");
printk("cache miss\n");
#endif
out:
spin_unlock(&sbi->cache_lock);
}
#ifdef DEBUG
static void list_cache(void)
static void list_cache(struct super_block *sb)
{
struct msdos_sb_info *sbi = MSDOS_SB(sb);
struct fat_cache *walk;
for (walk = fat_cache; walk; walk = walk->next) {
if (walk->sb)
printk("<%s,%d>(%d,%d) ", walk->sb->s_id,
for (walk = sbi->cache; walk; walk = walk->next) {
if (walk->start_cluster)
printk("<%s,%d>(%d,%d) ", sb->s_id,
walk->start_cluster, walk->file_cluster,
walk->disk_cluster);
else printk("-- ");
else
printk("-- ");
}
printk("\n");
}
#endif
void fat_cache_add(struct inode *inode,int f_clu,int d_clu)
/*
* Cache invalidation occurs rarely, thus the LRU chain is not updated. It
* fixes itself after a while.
*/
static void __fat_cache_inval_inode(struct inode *inode)
{
struct fat_cache *walk,*last;
struct fat_cache *walk;
int first = MSDOS_I(inode)->i_start;
for (walk = MSDOS_SB(inode->i_sb)->cache; walk; walk = walk->next)
if (walk->start_cluster == first)
walk->start_cluster = 0;
}
void fat_cache_inval_inode(struct inode *inode)
{
struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
spin_lock(&sbi->cache_lock);
__fat_cache_inval_inode(inode);
spin_unlock(&sbi->cache_lock);
}
void fat_cache_add(struct inode *inode, int f_clu, int d_clu)
{
struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
struct fat_cache *walk, *last;
int first;
first = MSDOS_I(inode)->i_start;
if (!first)
return;
last = NULL;
spin_lock(&fat_cache_lock);
for (walk = fat_cache; walk->next; walk = (last = walk)->next)
if (inode->i_sb == walk->sb
&& walk->start_cluster == first
&& walk->file_cluster == f_clu) {
spin_lock(&sbi->cache_lock);
for (walk = sbi->cache; walk->next; walk = (last = walk)->next) {
if (walk->start_cluster == first &&
walk->file_cluster == f_clu) {
if (walk->disk_cluster != d_clu) {
printk(KERN_ERR "FAT: cache corruption"
" (ino %lu)\n", inode->i_ino);
spin_unlock(&fat_cache_lock);
fat_cache_inval_inode(inode);
return;
__fat_cache_inval_inode(inode);
goto out;
}
if (last == NULL)
goto out;
/* update LRU */
if (last == NULL) {
spin_unlock(&fat_cache_lock);
return;
}
last->next = walk->next;
walk->next = fat_cache;
fat_cache = walk;
walk->next = sbi->cache;
sbi->cache = walk;
#ifdef DEBUG
list_cache();
list_cache();
#endif
spin_unlock(&fat_cache_lock);
return;
goto out;
}
walk->sb = inode->i_sb;
}
walk->start_cluster = first;
walk->file_cluster = f_clu;
walk->disk_cluster = d_clu;
last->next = NULL;
walk->next = fat_cache;
fat_cache = walk;
spin_unlock(&fat_cache_lock);
walk->next = sbi->cache;
sbi->cache = walk;
#ifdef DEBUG
list_cache();
list_cache();
#endif
out:
spin_unlock(&sbi->cache_lock);
}
/* Cache invalidation occurs rarely, thus the LRU chain is not updated. It
fixes itself after a while. */
void fat_cache_inval_inode(struct inode *inode)
{
struct fat_cache *walk;
int first = MSDOS_I(inode)->i_start;
spin_lock(&fat_cache_lock);
for (walk = fat_cache; walk; walk = walk->next)
if (walk->sb == inode->i_sb
&& walk->start_cluster == first)
walk->sb = NULL;
spin_unlock(&fat_cache_lock);
}
void fat_cache_inval_dev(struct super_block *sb)
{
struct fat_cache *walk;
spin_lock(&fat_cache_lock);
for (walk = fat_cache; walk; walk = walk->next)
if (walk->sb == sb)
walk->sb = 0;
spin_unlock(&fat_cache_lock);
}
static int fat_get_cluster(struct inode *inode, int cluster)
{
struct super_block *sb = inode->i_sb;
......
......@@ -160,7 +160,6 @@ void fat_put_super(struct super_block *sb)
struct msdos_sb_info *sbi = MSDOS_SB(sb);
fat_clusters_flush(sb);
fat_cache_inval_dev(sb);
if (sbi->nls_disk) {
unload_nls(sbi->nls_disk);
sbi->nls_disk = NULL;
......@@ -766,7 +765,7 @@ int fat_fill_super(struct super_block *sb, void *data, int silent,
if (!parse_options(data, isvfat, &debug, &sbi->options))
goto out_fail;
fat_cache_init();
fat_cache_init(sb);
/* set up enough so that it can read an inode */
init_MUTEX(&sbi->fat_lock);
......
......@@ -27,8 +27,6 @@ struct statfs;
#define MSDOS_SUPER_MAGIC 0x4d44 /* MD */
#define FAT_CACHE 8 /* FAT cache size */
#define ATTR_NONE 0 /* no attribute bits */
#define ATTR_RO 1 /* read-only */
#define ATTR_HIDDEN 2 /* hidden */
......@@ -204,14 +202,6 @@ static inline struct msdos_inode_info *MSDOS_I(struct inode *inode)
return container_of(inode, struct msdos_inode_info, vfs_inode);
}
struct fat_cache {
struct super_block *sb; /* fs in question. NULL means unused */
int start_cluster; /* first cluster of the chain. */
int file_cluster; /* cluster number in the file. */
int disk_cluster; /* cluster number on disk. */
struct fat_cache *next; /* next cache entry */
};
static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len)
{
#ifdef __BIG_ENDIAN
......@@ -242,12 +232,11 @@ static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len)
extern int fat_access(struct super_block *sb, int nr, int new_value);
extern int __fat_access(struct super_block *sb, int nr, int new_value);
extern int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys);
extern void fat_cache_init(void);
extern void fat_cache_init(struct super_block *sb);
extern void fat_cache_lookup(struct inode *inode, int cluster, int *f_clu,
int *d_clu);
extern void fat_cache_add(struct inode *inode, int f_clu, int d_clu);
extern void fat_cache_inval_inode(struct inode *inode);
extern void fat_cache_inval_dev(struct super_block *sb);
extern int fat_free(struct inode *inode, int skip);
/* fat/dir.c */
......
......@@ -26,6 +26,15 @@ struct fat_mount_options {
nocase:1; /* Does this need case conversion? 0=need case conversion*/
};
#define FAT_CACHE_NR 8 /* number of FAT cache */
struct fat_cache {
int start_cluster; /* first cluster of the chain. */
int file_cluster; /* cluster number in the file. */
int disk_cluster; /* cluster number on disk. */
struct fat_cache *next; /* next cache entry */
};
struct msdos_sb_info {
unsigned short cluster_size; /* sectors/cluster */
unsigned short cluster_bits; /* sectors/cluster */
......@@ -47,6 +56,9 @@ struct msdos_sb_info {
void *dir_ops; /* Opaque; default directory operations */
int dir_per_block; /* dir entries per block */
int dir_per_block_bits; /* log2(dir_per_block) */
spinlock_t cache_lock;
struct fat_cache cache_array[FAT_CACHE_NR], *cache;
};
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment