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 //#define MAXFATHEADTEST 1000000
17 int __fat_access(struct super_block
*sb
, int nr
, int new_value
)
19 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
20 struct buffer_head
*bh
, *bh2
, *c_bh
, *c_bh2
;
21 unsigned char *p_first
, *p_last
;
22 int copy
, first
, last
, next
, b
;
24 if (sbi
->fat_bits
== 32) {
26 } else if (sbi
->fat_bits
== 16) {
32 b
= sbi
->fat_start
+ (first
>> sb
->s_blocksize_bits
);
33 if (!(bh
= sb_bread(sb
, b
))) {
34 printk(KERN_ERR
"FAT: bread(block %d) in"
35 " fat_access failed\n", b
);
38 if ((first
>> sb
->s_blocksize_bits
) == (last
>> sb
->s_blocksize_bits
)) {
41 if (!(bh2
= sb_bread(sb
, b
+ 1))) {
43 printk(KERN_ERR
"FAT: bread(block %d) in"
44 " fat_access failed\n", b
+ 1);
48 if (sbi
->fat_bits
== 32) {
49 p_first
= p_last
= NULL
; /* GCC needs that stuff */
50 next
= CF_LE_L(((__le32
*) bh
->b_data
)[(first
&
51 (sb
->s_blocksize
- 1)) >> 2]);
52 /* Fscking Microsoft marketing department. Their "32" is 28. */
54 } else if (sbi
->fat_bits
== 16) {
55 p_first
= p_last
= NULL
; /* GCC needs that stuff */
56 next
= CF_LE_W(((__le16
*) bh
->b_data
)[(first
&
57 (sb
->s_blocksize
- 1)) >> 1]);
59 p_first
= &((__u8
*)bh
->b_data
)[first
& (sb
->s_blocksize
- 1)];
60 p_last
= &((__u8
*)bh2
->b_data
)[(first
+ 1) & (sb
->s_blocksize
- 1)];
62 next
= ((*p_first
>> 4) | (*p_last
<< 4)) & 0xfff;
64 next
= (*p_first
+(*p_last
<< 8)) & 0xfff;
66 if (new_value
!= -1) {
67 if (sbi
->fat_bits
== 32) {
68 ((__le32
*)bh
->b_data
)[(first
& (sb
->s_blocksize
- 1)) >> 2]
70 } else if (sbi
->fat_bits
== 16) {
71 ((__le16
*)bh
->b_data
)[(first
& (sb
->s_blocksize
- 1)) >> 1]
75 *p_first
= (*p_first
& 0xf) | (new_value
<< 4);
76 *p_last
= new_value
>> 4;
79 *p_first
= new_value
& 0xff;
80 *p_last
= (*p_last
& 0xf0) | (new_value
>> 8);
82 mark_buffer_dirty(bh2
);
84 mark_buffer_dirty(bh
);
85 for (copy
= 1; copy
< sbi
->fats
; copy
++) {
86 b
= sbi
->fat_start
+ (first
>> sb
->s_blocksize_bits
)
87 + sbi
->fat_length
* copy
;
88 if (!(c_bh
= sb_bread(sb
, b
)))
91 if (!(c_bh2
= sb_bread(sb
, b
+1))) {
95 memcpy(c_bh2
->b_data
, bh2
->b_data
, sb
->s_blocksize
);
96 mark_buffer_dirty(c_bh2
);
99 memcpy(c_bh
->b_data
, bh
->b_data
, sb
->s_blocksize
);
100 mark_buffer_dirty(c_bh
);
111 * Returns the this'th FAT entry, -1 if it is an end-of-file entry. If
112 * new_value is != -1, that FAT entry is replaced by it.
114 int fat_access(struct super_block
*sb
, int nr
, int new_value
)
126 if (nr
< 2 || MSDOS_SB(sb
)->clusters
+ 2 <= nr
) {
129 //if ( count <= MAXFATHEADTEST )
132 fat_fs_panic(sb
, "invalid access to FAT (entry 0x%08x)", nr
);
135 if (new_value
== FAT_ENT_EOF
)
136 new_value
= EOF_FAT(sb
);
138 next
= __fat_access(sb
, nr
, new_value
);
141 if (next
>= BAD_FAT(sb
))
147 void fat_cache_init(struct super_block
*sb
)
149 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
152 spin_lock_init(&sbi
->cache_lock
);
154 for (count
= 0; count
< FAT_CACHE_NR
- 1; count
++) {
155 sbi
->cache_array
[count
].start_cluster
= 0;
156 sbi
->cache_array
[count
].next
= &sbi
->cache_array
[count
+ 1];
158 sbi
->cache_array
[count
].start_cluster
= 0;
159 sbi
->cache_array
[count
].next
= NULL
;
160 sbi
->cache
= sbi
->cache_array
;
164 fat_cache_lookup(struct inode
*inode
, int cluster
, int *f_clu
, int *d_clu
)
166 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
167 struct fat_cache
*walk
;
170 BUG_ON(cluster
== 0);
172 first
= MSDOS_I(inode
)->i_start
;
176 spin_lock(&sbi
->cache_lock
);
178 if (MSDOS_I(inode
)->disk_cluster
&&
179 MSDOS_I(inode
)->file_cluster
<= cluster
) {
180 *d_clu
= MSDOS_I(inode
)->disk_cluster
;
181 *f_clu
= MSDOS_I(inode
)->file_cluster
;
184 for (walk
= sbi
->cache
; walk
; walk
= walk
->next
) {
185 if (walk
->start_cluster
== first
186 && walk
->file_cluster
<= cluster
187 && walk
->file_cluster
> *f_clu
) {
188 *d_clu
= walk
->disk_cluster
;
189 *f_clu
= walk
->file_cluster
;
191 printk("cache hit: %d (%d)\n", *f_clu
, *d_clu
);
193 if (*f_clu
== cluster
)
198 printk("cache miss\n");
201 spin_unlock(&sbi
->cache_lock
);
205 static void list_cache(struct super_block
*sb
)
207 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
208 struct fat_cache
*walk
;
210 for (walk
= sbi
->cache
; walk
; walk
= walk
->next
) {
211 if (walk
->start_cluster
)
212 printk("<%s,%d>(%d,%d) ", sb
->s_id
,
213 walk
->start_cluster
, walk
->file_cluster
,
223 * Cache invalidation occurs rarely, thus the LRU chain is not updated. It
224 * fixes itself after a while.
226 static void __fat_cache_inval_inode(struct inode
*inode
)
228 struct fat_cache
*walk
;
229 int first
= MSDOS_I(inode
)->i_start
;
230 MSDOS_I(inode
)->file_cluster
= MSDOS_I(inode
)->disk_cluster
= 0;
231 for (walk
= MSDOS_SB(inode
->i_sb
)->cache
; walk
; walk
= walk
->next
)
232 if (walk
->start_cluster
== first
)
233 walk
->start_cluster
= 0;
236 void fat_cache_inval_inode(struct inode
*inode
)
238 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
239 spin_lock(&sbi
->cache_lock
);
240 __fat_cache_inval_inode(inode
);
241 spin_unlock(&sbi
->cache_lock
);
244 void fat_cache_add(struct inode
*inode
, int f_clu
, int d_clu
)
246 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
247 struct fat_cache
*walk
, *last
;
248 int first
, prev_f_clu
, prev_d_clu
;
252 first
= MSDOS_I(inode
)->i_start
;
257 spin_lock(&sbi
->cache_lock
);
259 if (MSDOS_I(inode
)->file_cluster
== f_clu
)
262 prev_f_clu
= MSDOS_I(inode
)->file_cluster
;
263 prev_d_clu
= MSDOS_I(inode
)->disk_cluster
;
264 MSDOS_I(inode
)->file_cluster
= f_clu
;
265 MSDOS_I(inode
)->disk_cluster
= d_clu
;
272 for (walk
= sbi
->cache
; walk
->next
; walk
= (last
= walk
)->next
) {
273 if (walk
->start_cluster
== first
&&
274 walk
->file_cluster
== f_clu
) {
275 if (walk
->disk_cluster
!= d_clu
) {
276 printk(KERN_ERR
"FAT: cache corruption "
277 "(i_pos %lld)\n", MSDOS_I(inode
)->i_pos
);
278 __fat_cache_inval_inode(inode
);
285 last
->next
= walk
->next
;
286 walk
->next
= sbi
->cache
;
294 walk
->start_cluster
= first
;
295 walk
->file_cluster
= f_clu
;
296 walk
->disk_cluster
= d_clu
;
298 walk
->next
= sbi
->cache
;
304 spin_unlock(&sbi
->cache_lock
);
307 int fat_get_cluster(struct inode
*inode
, int cluster
, int *fclus
, int *dclus
)
309 struct super_block
*sb
= inode
->i_sb
;
310 const int limit
= sb
->s_maxbytes
>> MSDOS_SB(sb
)->cluster_bits
;
314 BUG_ON(MSDOS_I(inode
)->i_start
== 0);
317 *dclus
= MSDOS_I(inode
)->i_start
;
321 fat_cache_lookup(inode
, cluster
, fclus
, dclus
);
322 while (*fclus
< cluster
) {
323 /* prevent the infinite loop of cluster chain */
324 if (*fclus
> limit
) {
325 fat_fs_panic(sb
, "%s: detected the cluster chain loop"
326 " (i_pos %lld)", __FUNCTION__
,
327 MSDOS_I(inode
)->i_pos
);
331 nr
= fat_access(sb
, *dclus
, -1);
334 else if (nr
== FAT_ENT_FREE
) {
335 fat_fs_panic(sb
, "%s: invalid cluster chain"
336 " (i_pos %lld)", __FUNCTION__
,
337 MSDOS_I(inode
)->i_pos
);
339 } else if (nr
== FAT_ENT_EOF
) {
340 fat_cache_add(inode
, *fclus
, *dclus
);
346 fat_cache_add(inode
, *fclus
, *dclus
);
350 static int fat_bmap_cluster(struct inode
*inode
, int cluster
)
352 struct super_block
*sb
= inode
->i_sb
;
353 int ret
, fclus
, dclus
;
355 if (MSDOS_I(inode
)->i_start
== 0)
358 ret
= fat_get_cluster(inode
, cluster
, &fclus
, &dclus
);
361 else if (ret
== FAT_ENT_EOF
) {
362 fat_fs_panic(sb
, "%s: request beyond EOF (i_pos %lld)",
363 __FUNCTION__
, MSDOS_I(inode
)->i_pos
);
369 int fat_bmap(struct inode
*inode
, sector_t sector
, sector_t
*phys
)
371 struct super_block
*sb
= inode
->i_sb
;
372 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
377 if ((sbi
->fat_bits
!= 32) &&
378 (inode
->i_ino
== MSDOS_ROOT_INO
|| (S_ISDIR(inode
->i_mode
) &&
379 !MSDOS_I(inode
)->i_start
))) {
380 if (sector
< (sbi
->dir_entries
>> sbi
->dir_per_block_bits
))
381 *phys
= sector
+ sbi
->dir_start
;
384 last_block
= (MSDOS_I(inode
)->mmu_private
+ (sb
->s_blocksize
- 1))
385 >> sb
->s_blocksize_bits
;
386 if (sector
>= last_block
)
389 cluster
= sector
>> (sbi
->cluster_bits
- sb
->s_blocksize_bits
);
390 offset
= sector
& (sbi
->sec_per_clus
- 1);
391 cluster
= fat_bmap_cluster(inode
, cluster
);
395 *phys
= ((sector_t
)cluster
- 2) * sbi
->sec_per_clus
396 + sbi
->data_start
+ offset
;
401 /* Free all clusters after the skip'th cluster. */
402 int fat_free(struct inode
*inode
, int skip
)
404 struct super_block
*sb
= inode
->i_sb
;
405 int nr
, ret
, fclus
, dclus
;
410 if (MSDOS_I(inode
)->i_start
== 0)
414 ret
= fat_get_cluster(inode
, skip
- 1, &fclus
, &dclus
);
417 else if (ret
== FAT_ENT_EOF
)
420 nr
= fat_access(sb
, dclus
, -1);
421 if (nr
== FAT_ENT_EOF
)
425 * write a new EOF, and get the remaining cluster
428 nr
= fat_access(sb
, dclus
, FAT_ENT_EOF
);
433 fat_cache_inval_inode(inode
);
435 fat_cache_inval_inode(inode
);
437 nr
= MSDOS_I(inode
)->i_start
;
438 MSDOS_I(inode
)->i_start
= 0;
439 MSDOS_I(inode
)->i_logstart
= 0;
440 mark_inode_dirty(inode
);
447 nr
= fat_access(sb
, nr
, FAT_ENT_FREE
);
450 else if (nr
== FAT_ENT_FREE
) {
453 //if ( count <= MAXFATHEADTEST )
456 fat_fs_panic(sb
, "%s: deleting beyond EOF (i_pos %lld)",
457 __FUNCTION__
, MSDOS_I(inode
)->i_pos
);
461 if (MSDOS_SB(sb
)->free_clusters
!= -1)
462 MSDOS_SB(sb
)->free_clusters
++;
463 inode
->i_blocks
-= MSDOS_SB(sb
)->cluster_size
>> 9;
464 } while (nr
!= FAT_ENT_EOF
);
465 fat_clusters_flush(sb
);