4 * Written 1992,1993 by Werner Almesberger
6 * Mar 1999. AV. Changed cache, so that it uses the starting cluster instead
8 * May 1999. AV. Fixed the bogosity with FAT32 (read "FAT28"). Fscking lusers.
11 #include <linux/slab.h>
14 /* this must be > 0. */
15 #define FAT_MAX_CACHE 8
18 struct list_head cache_list;
19 int nr_contig; /* number of contiguous clusters */
20 int fcluster; /* cluster number in the file. */
21 int dcluster; /* cluster number on disk. */
31 static inline int fat_max_cache(struct inode *inode)
36 static struct kmem_cache *fat_cache_cachep;
38 static void init_once(void *foo)
40 struct fat_cache *cache = (struct fat_cache *)foo;
42 INIT_LIST_HEAD(&cache->cache_list);
45 int __init fat_cache_init(void)
47 fat_cache_cachep = kmem_cache_create("fat_cache",
48 sizeof(struct fat_cache),
49 0, SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
51 if (fat_cache_cachep == NULL)
56 void fat_cache_destroy(void)
58 kmem_cache_destroy(fat_cache_cachep);
61 static inline struct fat_cache *fat_cache_alloc(struct inode *inode)
63 return kmem_cache_alloc(fat_cache_cachep, GFP_NOFS);
66 static inline void fat_cache_free(struct fat_cache *cache)
68 BUG_ON(!list_empty(&cache->cache_list));
69 kmem_cache_free(fat_cache_cachep, cache);
72 static inline void fat_cache_update_lru(struct inode *inode,
73 struct fat_cache *cache)
75 if (MSDOS_I(inode)->cache_lru.next != &cache->cache_list)
76 list_move(&cache->cache_list, &MSDOS_I(inode)->cache_lru);
79 static int fat_cache_lookup(struct inode *inode, int fclus,
80 struct fat_cache_id *cid,
81 int *cached_fclus, int *cached_dclus)
83 static struct fat_cache nohit = { .fcluster = 0, };
85 struct fat_cache *hit = &nohit, *p;
88 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
89 list_for_each_entry(p, &MSDOS_I(inode)->cache_lru, cache_list) {
90 /* Find the cache of "fclus" or nearest cache. */
91 if (p->fcluster <= fclus && hit->fcluster < p->fcluster) {
93 if ((hit->fcluster + hit->nr_contig) < fclus) {
94 offset = hit->nr_contig;
96 offset = fclus - hit->fcluster;
102 fat_cache_update_lru(inode, hit);
104 cid->id = MSDOS_I(inode)->cache_valid_id;
105 cid->nr_contig = hit->nr_contig;
106 cid->fcluster = hit->fcluster;
107 cid->dcluster = hit->dcluster;
108 *cached_fclus = cid->fcluster + offset;
109 *cached_dclus = cid->dcluster + offset;
111 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
116 static struct fat_cache *fat_cache_merge(struct inode *inode,
117 struct fat_cache_id *new)
121 list_for_each_entry(p, &MSDOS_I(inode)->cache_lru, cache_list) {
122 /* Find the same part as "new" in cluster-chain. */
123 if (p->fcluster == new->fcluster) {
124 BUG_ON(p->dcluster != new->dcluster);
125 if (new->nr_contig > p->nr_contig)
126 p->nr_contig = new->nr_contig;
133 static void fat_cache_add(struct inode *inode, struct fat_cache_id *new)
135 struct fat_cache *cache, *tmp;
137 if (new->fcluster == -1) /* dummy cache */
140 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
141 if (new->id != FAT_CACHE_VALID &&
142 new->id != MSDOS_I(inode)->cache_valid_id)
143 goto out; /* this cache was invalidated */
145 cache = fat_cache_merge(inode, new);
147 if (MSDOS_I(inode)->nr_caches < fat_max_cache(inode)) {
148 MSDOS_I(inode)->nr_caches++;
149 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
151 tmp = fat_cache_alloc(inode);
153 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
154 MSDOS_I(inode)->nr_caches--;
155 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
159 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
160 cache = fat_cache_merge(inode, new);
162 MSDOS_I(inode)->nr_caches--;
168 struct list_head *p = MSDOS_I(inode)->cache_lru.prev;
169 cache = list_entry(p, struct fat_cache, cache_list);
171 cache->fcluster = new->fcluster;
172 cache->dcluster = new->dcluster;
173 cache->nr_contig = new->nr_contig;
176 fat_cache_update_lru(inode, cache);
178 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
182 * Cache invalidation occurs rarely, thus the LRU chain is not updated. It
183 * fixes itself after a while.
185 static void __fat_cache_inval_inode(struct inode *inode)
187 struct msdos_inode_info *i = MSDOS_I(inode);
188 struct fat_cache *cache;
190 while (!list_empty(&i->cache_lru)) {
191 cache = list_entry(i->cache_lru.next,
192 struct fat_cache, cache_list);
193 list_del_init(&cache->cache_list);
195 fat_cache_free(cache);
197 /* Update. The copy of caches before this id is discarded. */
199 if (i->cache_valid_id == FAT_CACHE_VALID)
203 void fat_cache_inval_inode(struct inode *inode)
205 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
206 __fat_cache_inval_inode(inode);
207 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
210 static inline int cache_contiguous(struct fat_cache_id *cid, int dclus)
213 return ((cid->dcluster + cid->nr_contig) == dclus);
216 static inline void cache_init(struct fat_cache_id *cid, int fclus, int dclus)
218 cid->id = FAT_CACHE_VALID;
219 cid->fcluster = fclus;
220 cid->dcluster = dclus;
224 int fat_get_cluster(struct inode *inode, int cluster, int *fclus, int *dclus)
226 struct super_block *sb = inode->i_sb;
227 struct msdos_sb_info *sbi = MSDOS_SB(sb);
228 const int limit = sb->s_maxbytes >> sbi->cluster_bits;
229 struct fat_entry fatent;
230 struct fat_cache_id cid;
233 BUG_ON(MSDOS_I(inode)->i_start == 0);
236 *dclus = MSDOS_I(inode)->i_start;
237 if (!fat_valid_entry(sbi, *dclus)) {
238 fat_fs_error_ratelimit(sb,
239 "%s: invalid start cluster (i_pos %lld, start %08x)",
240 __func__, MSDOS_I(inode)->i_pos, *dclus);
246 if (fat_cache_lookup(inode, cluster, &cid, fclus, dclus) < 0) {
248 * dummy, always not contiguous
249 * This is reinitialized by cache_init(), later.
251 cache_init(&cid, -1, -1);
254 fatent_init(&fatent);
255 while (*fclus < cluster) {
256 /* prevent the infinite loop of cluster chain */
257 if (*fclus > limit) {
258 fat_fs_error_ratelimit(sb,
259 "%s: detected the cluster chain loop (i_pos %lld)",
260 __func__, MSDOS_I(inode)->i_pos);
265 nr = fat_ent_read(inode, &fatent, *dclus);
268 else if (nr == FAT_ENT_FREE) {
269 fat_fs_error_ratelimit(sb,
270 "%s: invalid cluster chain (i_pos %lld)",
271 __func__, MSDOS_I(inode)->i_pos);
274 } else if (nr == FAT_ENT_EOF) {
275 fat_cache_add(inode, &cid);
280 if (!cache_contiguous(&cid, *dclus))
281 cache_init(&cid, *fclus, *dclus);
284 fat_cache_add(inode, &cid);
286 fatent_brelse(&fatent);
290 static int fat_bmap_cluster(struct inode *inode, int cluster)
292 struct super_block *sb = inode->i_sb;
293 int ret, fclus, dclus;
295 if (MSDOS_I(inode)->i_start == 0)
298 ret = fat_get_cluster(inode, cluster, &fclus, &dclus);
301 else if (ret == FAT_ENT_EOF) {
302 fat_fs_error(sb, "%s: request beyond EOF (i_pos %lld)",
303 __func__, MSDOS_I(inode)->i_pos);
309 int fat_get_mapped_cluster(struct inode *inode, sector_t sector,
311 unsigned long *mapped_blocks, sector_t *bmap)
313 struct super_block *sb = inode->i_sb;
314 struct msdos_sb_info *sbi = MSDOS_SB(sb);
317 cluster = sector >> (sbi->cluster_bits - sb->s_blocksize_bits);
318 offset = sector & (sbi->sec_per_clus - 1);
319 cluster = fat_bmap_cluster(inode, cluster);
323 *bmap = fat_clus_to_blknr(sbi, cluster) + offset;
324 *mapped_blocks = sbi->sec_per_clus - offset;
325 if (*mapped_blocks > last_block - sector)
326 *mapped_blocks = last_block - sector;
332 static int is_exceed_eof(struct inode *inode, sector_t sector,
333 sector_t *last_block, int create)
335 struct super_block *sb = inode->i_sb;
336 const unsigned long blocksize = sb->s_blocksize;
337 const unsigned char blocksize_bits = sb->s_blocksize_bits;
339 *last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
340 if (sector >= *last_block) {
345 * ->mmu_private can access on only allocation path.
346 * (caller must hold ->i_mutex)
348 *last_block = (MSDOS_I(inode)->mmu_private + (blocksize - 1))
350 if (sector >= *last_block)
357 int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
358 unsigned long *mapped_blocks, int create, bool from_bmap)
360 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
365 if ((sbi->fat_bits != 32) && (inode->i_ino == MSDOS_ROOT_INO)) {
366 if (sector < (sbi->dir_entries >> sbi->dir_per_block_bits)) {
367 *phys = sector + sbi->dir_start;
374 if (is_exceed_eof(inode, sector, &last_block, create))
377 last_block = inode->i_blocks >>
378 (inode->i_sb->s_blocksize_bits - 9);
379 if (sector >= last_block)
383 return fat_get_mapped_cluster(inode, sector, last_block, mapped_blocks,