12 #include <linux/slab.h>
17 #define FAT_MAX_CACHE 8
33 static inline int fat_max_cache(
struct inode *
inode)
40 static void init_once(
void *
foo)
53 if (fat_cache_cachep ==
NULL)
74 static inline void fat_cache_update_lru(
struct inode *
inode,
77 if (MSDOS_I(inode)->cache_lru.next != &cache->
cache_list)
78 list_move(&cache->
cache_list, &MSDOS_I(inode)->cache_lru);
81 static int fat_cache_lookup(
struct inode *inode,
int fclus,
83 int *cached_fclus,
int *cached_dclus)
90 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
93 if (p->fcluster <= fclus && hit->
fcluster < p->fcluster) {
104 fat_cache_update_lru(inode, hit);
106 cid->
id = MSDOS_I(inode)->cache_valid_id;
113 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
118 static struct fat_cache *fat_cache_merge(
struct inode *inode,
135 static void fat_cache_add(
struct inode *inode,
struct fat_cache_id *
new)
139 if (new->fcluster == -1)
142 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
144 new->id != MSDOS_I(inode)->cache_valid_id)
147 cache = fat_cache_merge(inode,
new);
149 if (MSDOS_I(inode)->nr_caches < fat_max_cache(inode)) {
150 MSDOS_I(inode)->nr_caches++;
151 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
153 tmp = fat_cache_alloc(inode);
155 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
156 MSDOS_I(inode)->nr_caches--;
157 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
161 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
162 cache = fat_cache_merge(inode,
new);
164 MSDOS_I(inode)->nr_caches--;
170 struct list_head *p = MSDOS_I(inode)->cache_lru.prev;
178 fat_cache_update_lru(inode, cache);
180 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
187 static void __fat_cache_inval_inode(
struct inode *inode)
197 fat_cache_free(cache);
207 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
208 __fat_cache_inval_inode(inode);
209 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
212 static inline int cache_contiguous(
struct fat_cache_id *cid,
int dclus)
234 BUG_ON(MSDOS_I(inode)->i_start == 0);
237 *dclus = MSDOS_I(inode)->i_start;
241 if (fat_cache_lookup(inode, cluster, &cid, fclus, dclus) < 0) {
249 fatent_init(&fatent);
250 while (*fclus < cluster) {
252 if (*fclus > limit) {
254 "%s: detected the cluster chain loop"
255 " (i_pos %lld)", __func__,
256 MSDOS_I(inode)->i_pos);
266 "%s: invalid cluster chain (i_pos %lld)",
268 MSDOS_I(inode)->i_pos);
272 fat_cache_add(inode, &cid);
277 if (!cache_contiguous(&cid, *dclus))
281 fat_cache_add(inode, &cid);
283 fatent_brelse(&fatent);
287 static int fat_bmap_cluster(
struct inode *inode,
int cluster)
290 int ret, fclus, dclus;
292 if (MSDOS_I(inode)->i_start == 0)
300 __func__, MSDOS_I(inode)->i_pos);
307 unsigned long *mapped_blocks,
int create)
326 last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
327 if (sector >= last_block) {
335 last_block = (MSDOS_I(inode)->mmu_private + (blocksize - 1))
337 if (sector >= last_block)
343 cluster = fat_bmap_cluster(inode, cluster);
347 *phys = fat_clus_to_blknr(sbi, cluster) +
offset;
349 if (*mapped_blocks > last_block - sector)
350 *mapped_blocks = last_block -
sector;