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/slab.h>
13 #include <linux/buffer_head.h>
16 /* this must be > 0. */
17 #define FAT_MAX_CACHE 8
20 struct list_head cache_list
;
21 int nr_contig
; /* number of contiguous clusters */
22 int fcluster
; /* cluster number in the file. */
23 int dcluster
; /* cluster number on disk. */
33 static inline int fat_max_cache(struct inode
*inode
)
38 static struct kmem_cache
*fat_cache_cachep
;
40 static void init_once(void *foo
)
42 struct fat_cache
*cache
= (struct fat_cache
*)foo
;
44 INIT_LIST_HEAD(&cache
->cache_list
);
47 int __init
fat_cache_init(void)
49 fat_cache_cachep
= kmem_cache_create("fat_cache",
50 sizeof(struct fat_cache
),
51 0, SLAB_RECLAIM_ACCOUNT
|SLAB_MEM_SPREAD
,
53 if (fat_cache_cachep
== NULL
)
58 void fat_cache_destroy(void)
60 kmem_cache_destroy(fat_cache_cachep
);
63 static inline struct fat_cache
*fat_cache_alloc(struct inode
*inode
)
65 return kmem_cache_alloc(fat_cache_cachep
, GFP_NOFS
);
68 static inline void fat_cache_free(struct fat_cache
*cache
)
70 BUG_ON(!list_empty(&cache
->cache_list
));
71 kmem_cache_free(fat_cache_cachep
, cache
);
74 static inline void fat_cache_update_lru(struct inode
*inode
,
75 struct fat_cache
*cache
)
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
,
82 struct fat_cache_id
*cid
,
83 int *cached_fclus
, int *cached_dclus
)
85 static struct fat_cache nohit
= { .fcluster
= 0, };
87 struct fat_cache
*hit
= &nohit
, *p
;
90 spin_lock(&MSDOS_I(inode
)->cache_lru_lock
);
91 list_for_each_entry(p
, &MSDOS_I(inode
)->cache_lru
, cache_list
) {
92 /* Find the cache of "fclus" or nearest cache. */
93 if (p
->fcluster
<= fclus
&& hit
->fcluster
< p
->fcluster
) {
95 if ((hit
->fcluster
+ hit
->nr_contig
) < fclus
) {
96 offset
= hit
->nr_contig
;
98 offset
= fclus
- hit
->fcluster
;
104 fat_cache_update_lru(inode
, hit
);
106 cid
->id
= MSDOS_I(inode
)->cache_valid_id
;
107 cid
->nr_contig
= hit
->nr_contig
;
108 cid
->fcluster
= hit
->fcluster
;
109 cid
->dcluster
= hit
->dcluster
;
110 *cached_fclus
= cid
->fcluster
+ offset
;
111 *cached_dclus
= cid
->dcluster
+ offset
;
113 spin_unlock(&MSDOS_I(inode
)->cache_lru_lock
);
118 static struct fat_cache
*fat_cache_merge(struct inode
*inode
,
119 struct fat_cache_id
*new)
123 list_for_each_entry(p
, &MSDOS_I(inode
)->cache_lru
, cache_list
) {
124 /* Find the same part as "new" in cluster-chain. */
125 if (p
->fcluster
== new->fcluster
) {
126 BUG_ON(p
->dcluster
!= new->dcluster
);
127 if (new->nr_contig
> p
->nr_contig
)
128 p
->nr_contig
= new->nr_contig
;
135 static void fat_cache_add(struct inode
*inode
, struct fat_cache_id
*new)
137 struct fat_cache
*cache
, *tmp
;
139 if (new->fcluster
== -1) /* dummy cache */
142 spin_lock(&MSDOS_I(inode
)->cache_lru_lock
);
143 if (new->id
!= FAT_CACHE_VALID
&&
144 new->id
!= MSDOS_I(inode
)->cache_valid_id
)
145 goto out
; /* this cache was invalidated */
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
;
171 cache
= list_entry(p
, struct fat_cache
, cache_list
);
173 cache
->fcluster
= new->fcluster
;
174 cache
->dcluster
= new->dcluster
;
175 cache
->nr_contig
= new->nr_contig
;
178 fat_cache_update_lru(inode
, cache
);
180 spin_unlock(&MSDOS_I(inode
)->cache_lru_lock
);
184 * Cache invalidation occurs rarely, thus the LRU chain is not updated. It
185 * fixes itself after a while.
187 static void __fat_cache_inval_inode(struct inode
*inode
)
189 struct msdos_inode_info
*i
= MSDOS_I(inode
);
190 struct fat_cache
*cache
;
192 while (!list_empty(&i
->cache_lru
)) {
193 cache
= list_entry(i
->cache_lru
.next
, struct fat_cache
, cache_list
);
194 list_del_init(&cache
->cache_list
);
196 fat_cache_free(cache
);
198 /* Update. The copy of caches before this id is discarded. */
200 if (i
->cache_valid_id
== FAT_CACHE_VALID
)
204 void fat_cache_inval_inode(struct inode
*inode
)
206 spin_lock(&MSDOS_I(inode
)->cache_lru_lock
);
207 __fat_cache_inval_inode(inode
);
208 spin_unlock(&MSDOS_I(inode
)->cache_lru_lock
);
211 static inline int cache_contiguous(struct fat_cache_id
*cid
, int dclus
)
214 return ((cid
->dcluster
+ cid
->nr_contig
) == dclus
);
217 static inline void cache_init(struct fat_cache_id
*cid
, int fclus
, int dclus
)
219 cid
->id
= FAT_CACHE_VALID
;
220 cid
->fcluster
= fclus
;
221 cid
->dcluster
= dclus
;
225 int fat_get_cluster(struct inode
*inode
, int cluster
, int *fclus
, int *dclus
)
227 struct super_block
*sb
= inode
->i_sb
;
228 const int limit
= sb
->s_maxbytes
>> MSDOS_SB(sb
)->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
;
240 if (fat_cache_lookup(inode
, cluster
, &cid
, fclus
, dclus
) < 0) {
242 * dummy, always not contiguous
243 * This is reinitialized by cache_init(), later.
245 cache_init(&cid
, -1, -1);
248 fatent_init(&fatent
);
249 while (*fclus
< cluster
) {
250 /* prevent the infinite loop of cluster chain */
251 if (*fclus
> limit
) {
252 fat_fs_error_ratelimit(sb
,
253 "%s: detected the cluster chain loop"
254 " (i_pos %lld)", __func__
,
255 MSDOS_I(inode
)->i_pos
);
260 nr
= fat_ent_read(inode
, &fatent
, *dclus
);
263 else if (nr
== FAT_ENT_FREE
) {
264 fat_fs_error_ratelimit(sb
, "%s: invalid cluster chain"
265 " (i_pos %lld)", __func__
,
266 MSDOS_I(inode
)->i_pos
);
269 } else if (nr
== FAT_ENT_EOF
) {
270 fat_cache_add(inode
, &cid
);
275 if (!cache_contiguous(&cid
, *dclus
))
276 cache_init(&cid
, *fclus
, *dclus
);
279 fat_cache_add(inode
, &cid
);
281 fatent_brelse(&fatent
);
285 static int fat_bmap_cluster(struct inode
*inode
, int cluster
)
287 struct super_block
*sb
= inode
->i_sb
;
288 int ret
, fclus
, dclus
;
290 if (MSDOS_I(inode
)->i_start
== 0)
293 ret
= fat_get_cluster(inode
, cluster
, &fclus
, &dclus
);
296 else if (ret
== FAT_ENT_EOF
) {
297 fat_fs_error(sb
, "%s: request beyond EOF (i_pos %lld)",
298 __func__
, MSDOS_I(inode
)->i_pos
);
304 int fat_bmap(struct inode
*inode
, sector_t sector
, sector_t
*phys
,
305 unsigned long *mapped_blocks
, int create
)
307 struct super_block
*sb
= inode
->i_sb
;
308 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
309 const unsigned long blocksize
= sb
->s_blocksize
;
310 const unsigned char blocksize_bits
= sb
->s_blocksize_bits
;
316 if ((sbi
->fat_bits
!= 32) && (inode
->i_ino
== MSDOS_ROOT_INO
)) {
317 if (sector
< (sbi
->dir_entries
>> sbi
->dir_per_block_bits
)) {
318 *phys
= sector
+ sbi
->dir_start
;
324 last_block
= (i_size_read(inode
) + (blocksize
- 1)) >> blocksize_bits
;
325 if (sector
>= last_block
) {
330 * ->mmu_private can access on only allocation path.
331 * (caller must hold ->i_mutex)
333 last_block
= (MSDOS_I(inode
)->mmu_private
+ (blocksize
- 1))
335 if (sector
>= last_block
)
339 cluster
= sector
>> (sbi
->cluster_bits
- sb
->s_blocksize_bits
);
340 offset
= sector
& (sbi
->sec_per_clus
- 1);
341 cluster
= fat_bmap_cluster(inode
, cluster
);
345 *phys
= fat_clus_to_blknr(sbi
, cluster
) + offset
;
346 *mapped_blocks
= sbi
->sec_per_clus
- offset
;
347 if (*mapped_blocks
> last_block
- sector
)
348 *mapped_blocks
= last_block
- sector
;