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.
12 #include <linux/msdos_fs.h>
13 #include <linux/buffer_head.h>
15 /* this must be > 0. */
16 #define FAT_MAX_CACHE 8
19 struct list_head cache_list
;
20 int nr_contig
; /* number of contiguous clusters */
21 int fcluster
; /* cluster number in the file. */
22 int dcluster
; /* cluster number on disk. */
32 static inline int fat_max_cache(struct inode
*inode
)
37 static struct kmem_cache
*fat_cache_cachep
;
39 static void init_once(void *foo
, struct kmem_cache
*cachep
, unsigned long flags
)
41 struct fat_cache
*cache
= (struct fat_cache
*)foo
;
43 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
44 SLAB_CTOR_CONSTRUCTOR
)
45 INIT_LIST_HEAD(&cache
->cache_list
);
48 int __init
fat_cache_init(void)
50 fat_cache_cachep
= kmem_cache_create("fat_cache",
51 sizeof(struct fat_cache
),
52 0, SLAB_RECLAIM_ACCOUNT
|SLAB_MEM_SPREAD
,
54 if (fat_cache_cachep
== NULL
)
59 void fat_cache_destroy(void)
61 kmem_cache_destroy(fat_cache_cachep
);
64 static inline struct fat_cache
*fat_cache_alloc(struct inode
*inode
)
66 return kmem_cache_alloc(fat_cache_cachep
, GFP_KERNEL
);
69 static inline void fat_cache_free(struct fat_cache
*cache
)
71 BUG_ON(!list_empty(&cache
->cache_list
));
72 kmem_cache_free(fat_cache_cachep
, cache
);
75 static inline void fat_cache_update_lru(struct inode
*inode
,
76 struct fat_cache
*cache
)
78 if (MSDOS_I(inode
)->cache_lru
.next
!= &cache
->cache_list
)
79 list_move(&cache
->cache_list
, &MSDOS_I(inode
)->cache_lru
);
82 static int fat_cache_lookup(struct inode
*inode
, int fclus
,
83 struct fat_cache_id
*cid
,
84 int *cached_fclus
, int *cached_dclus
)
86 static struct fat_cache nohit
= { .fcluster
= 0, };
88 struct fat_cache
*hit
= &nohit
, *p
;
91 spin_lock(&MSDOS_I(inode
)->cache_lru_lock
);
92 list_for_each_entry(p
, &MSDOS_I(inode
)->cache_lru
, cache_list
) {
93 /* Find the cache of "fclus" or nearest cache. */
94 if (p
->fcluster
<= fclus
&& hit
->fcluster
< p
->fcluster
) {
96 if ((hit
->fcluster
+ hit
->nr_contig
) < fclus
) {
97 offset
= hit
->nr_contig
;
99 offset
= fclus
- hit
->fcluster
;
105 fat_cache_update_lru(inode
, hit
);
107 cid
->id
= MSDOS_I(inode
)->cache_valid_id
;
108 cid
->nr_contig
= hit
->nr_contig
;
109 cid
->fcluster
= hit
->fcluster
;
110 cid
->dcluster
= hit
->dcluster
;
111 *cached_fclus
= cid
->fcluster
+ offset
;
112 *cached_dclus
= cid
->dcluster
+ offset
;
114 spin_unlock(&MSDOS_I(inode
)->cache_lru_lock
);
119 static struct fat_cache
*fat_cache_merge(struct inode
*inode
,
120 struct fat_cache_id
*new)
124 list_for_each_entry(p
, &MSDOS_I(inode
)->cache_lru
, cache_list
) {
125 /* Find the same part as "new" in cluster-chain. */
126 if (p
->fcluster
== new->fcluster
) {
127 BUG_ON(p
->dcluster
!= new->dcluster
);
128 if (new->nr_contig
> p
->nr_contig
)
129 p
->nr_contig
= new->nr_contig
;
136 static void fat_cache_add(struct inode
*inode
, struct fat_cache_id
*new)
138 struct fat_cache
*cache
, *tmp
;
140 if (new->fcluster
== -1) /* dummy cache */
143 spin_lock(&MSDOS_I(inode
)->cache_lru_lock
);
144 if (new->id
!= FAT_CACHE_VALID
&&
145 new->id
!= MSDOS_I(inode
)->cache_valid_id
)
146 goto out
; /* this cache was invalidated */
148 cache
= fat_cache_merge(inode
, new);
150 if (MSDOS_I(inode
)->nr_caches
< fat_max_cache(inode
)) {
151 MSDOS_I(inode
)->nr_caches
++;
152 spin_unlock(&MSDOS_I(inode
)->cache_lru_lock
);
154 tmp
= fat_cache_alloc(inode
);
155 spin_lock(&MSDOS_I(inode
)->cache_lru_lock
);
156 cache
= fat_cache_merge(inode
, new);
158 MSDOS_I(inode
)->nr_caches
--;
164 struct list_head
*p
= MSDOS_I(inode
)->cache_lru
.prev
;
165 cache
= list_entry(p
, struct fat_cache
, cache_list
);
167 cache
->fcluster
= new->fcluster
;
168 cache
->dcluster
= new->dcluster
;
169 cache
->nr_contig
= new->nr_contig
;
172 fat_cache_update_lru(inode
, cache
);
174 spin_unlock(&MSDOS_I(inode
)->cache_lru_lock
);
178 * Cache invalidation occurs rarely, thus the LRU chain is not updated. It
179 * fixes itself after a while.
181 static void __fat_cache_inval_inode(struct inode
*inode
)
183 struct msdos_inode_info
*i
= MSDOS_I(inode
);
184 struct fat_cache
*cache
;
186 while (!list_empty(&i
->cache_lru
)) {
187 cache
= list_entry(i
->cache_lru
.next
, struct fat_cache
, cache_list
);
188 list_del_init(&cache
->cache_list
);
190 fat_cache_free(cache
);
192 /* Update. The copy of caches before this id is discarded. */
194 if (i
->cache_valid_id
== FAT_CACHE_VALID
)
198 void fat_cache_inval_inode(struct inode
*inode
)
200 spin_lock(&MSDOS_I(inode
)->cache_lru_lock
);
201 __fat_cache_inval_inode(inode
);
202 spin_unlock(&MSDOS_I(inode
)->cache_lru_lock
);
205 static inline int cache_contiguous(struct fat_cache_id
*cid
, int dclus
)
208 return ((cid
->dcluster
+ cid
->nr_contig
) == dclus
);
211 static inline void cache_init(struct fat_cache_id
*cid
, int fclus
, int dclus
)
213 cid
->id
= FAT_CACHE_VALID
;
214 cid
->fcluster
= fclus
;
215 cid
->dcluster
= dclus
;
219 int fat_get_cluster(struct inode
*inode
, int cluster
, int *fclus
, int *dclus
)
221 struct super_block
*sb
= inode
->i_sb
;
222 const int limit
= sb
->s_maxbytes
>> MSDOS_SB(sb
)->cluster_bits
;
223 struct fat_entry fatent
;
224 struct fat_cache_id cid
;
227 BUG_ON(MSDOS_I(inode
)->i_start
== 0);
230 *dclus
= MSDOS_I(inode
)->i_start
;
234 if (fat_cache_lookup(inode
, cluster
, &cid
, fclus
, dclus
) < 0) {
236 * dummy, always not contiguous
237 * This is reinitialized by cache_init(), later.
239 cache_init(&cid
, -1, -1);
242 fatent_init(&fatent
);
243 while (*fclus
< cluster
) {
244 /* prevent the infinite loop of cluster chain */
245 if (*fclus
> limit
) {
246 fat_fs_panic(sb
, "%s: detected the cluster chain loop"
247 " (i_pos %lld)", __FUNCTION__
,
248 MSDOS_I(inode
)->i_pos
);
253 nr
= fat_ent_read(inode
, &fatent
, *dclus
);
256 else if (nr
== FAT_ENT_FREE
) {
257 fat_fs_panic(sb
, "%s: invalid cluster chain"
258 " (i_pos %lld)", __FUNCTION__
,
259 MSDOS_I(inode
)->i_pos
);
262 } else if (nr
== FAT_ENT_EOF
) {
263 fat_cache_add(inode
, &cid
);
268 if (!cache_contiguous(&cid
, *dclus
))
269 cache_init(&cid
, *fclus
, *dclus
);
272 fat_cache_add(inode
, &cid
);
274 fatent_brelse(&fatent
);
278 static int fat_bmap_cluster(struct inode
*inode
, int cluster
)
280 struct super_block
*sb
= inode
->i_sb
;
281 int ret
, fclus
, dclus
;
283 if (MSDOS_I(inode
)->i_start
== 0)
286 ret
= fat_get_cluster(inode
, cluster
, &fclus
, &dclus
);
289 else if (ret
== FAT_ENT_EOF
) {
290 fat_fs_panic(sb
, "%s: request beyond EOF (i_pos %lld)",
291 __FUNCTION__
, MSDOS_I(inode
)->i_pos
);
297 int fat_bmap(struct inode
*inode
, sector_t sector
, sector_t
*phys
,
298 unsigned long *mapped_blocks
)
300 struct super_block
*sb
= inode
->i_sb
;
301 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
307 if ((sbi
->fat_bits
!= 32) && (inode
->i_ino
== MSDOS_ROOT_INO
)) {
308 if (sector
< (sbi
->dir_entries
>> sbi
->dir_per_block_bits
)) {
309 *phys
= sector
+ sbi
->dir_start
;
314 last_block
= (MSDOS_I(inode
)->mmu_private
+ (sb
->s_blocksize
- 1))
315 >> sb
->s_blocksize_bits
;
316 if (sector
>= last_block
)
319 cluster
= sector
>> (sbi
->cluster_bits
- sb
->s_blocksize_bits
);
320 offset
= sector
& (sbi
->sec_per_clus
- 1);
321 cluster
= fat_bmap_cluster(inode
, cluster
);
325 *phys
= fat_clus_to_blknr(sbi
, cluster
) + offset
;
326 *mapped_blocks
= sbi
->sec_per_clus
- offset
;
327 if (*mapped_blocks
> last_block
- sector
)
328 *mapped_blocks
= last_block
- sector
;