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 int __fat_access(struct super_block *sb, int nr, int new_value)
17 struct msdos_sb_info *sbi = MSDOS_SB(sb);
18 struct buffer_head *bh, *bh2, *c_bh, *c_bh2;
19 unsigned char *p_first, *p_last;
20 int copy, first, last, next, b;
22 if (sbi->fat_bits == 32) {
24 } else if (sbi->fat_bits == 16) {
30 b = sbi->fat_start + (first >> sb->s_blocksize_bits);
31 if (!(bh = sb_bread(sb, b))) {
32 printk(KERN_ERR "FAT: bread(block %d) in"
33 " fat_access failed\n", b);
36 if ((first >> sb->s_blocksize_bits) == (last >> sb->s_blocksize_bits)) {
39 if (!(bh2 = sb_bread(sb, b + 1))) {
41 printk(KERN_ERR "FAT: bread(block %d) in"
42 " fat_access failed\n", b + 1);
46 if (sbi->fat_bits == 32) {
47 p_first = p_last = NULL; /* GCC needs that stuff */
48 next = CF_LE_L(((__le32 *) bh->b_data)[(first &
49 (sb->s_blocksize - 1)) >> 2]);
50 /* Fscking Microsoft marketing department. Their "32" is 28. */
52 } else if (sbi->fat_bits == 16) {
53 p_first = p_last = NULL; /* GCC needs that stuff */
54 next = CF_LE_W(((__le16 *) bh->b_data)[(first &
55 (sb->s_blocksize - 1)) >> 1]);
57 p_first = &((__u8 *)bh->b_data)[first & (sb->s_blocksize - 1)];
58 p_last = &((__u8 *)bh2->b_data)[(first + 1) & (sb->s_blocksize - 1)];
60 next = ((*p_first >> 4) | (*p_last << 4)) & 0xfff;
62 next = (*p_first+(*p_last << 8)) & 0xfff;
64 if (new_value != -1) {
65 if (sbi->fat_bits == 32) {
66 ((__le32 *)bh->b_data)[(first & (sb->s_blocksize - 1)) >> 2]
68 } else if (sbi->fat_bits == 16) {
69 ((__le16 *)bh->b_data)[(first & (sb->s_blocksize - 1)) >> 1]
73 *p_first = (*p_first & 0xf) | (new_value << 4);
74 *p_last = new_value >> 4;
77 *p_first = new_value & 0xff;
78 *p_last = (*p_last & 0xf0) | (new_value >> 8);
80 mark_buffer_dirty(bh2);
82 mark_buffer_dirty(bh);
83 for (copy = 1; copy < sbi->fats; copy++) {
84 b = sbi->fat_start + (first >> sb->s_blocksize_bits)
85 + sbi->fat_length * copy;
86 if (!(c_bh = sb_bread(sb, b)))
89 if (!(c_bh2 = sb_bread(sb, b+1))) {
93 memcpy(c_bh2->b_data, bh2->b_data, sb->s_blocksize);
94 mark_buffer_dirty(c_bh2);
97 memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize);
98 mark_buffer_dirty(c_bh);
109 * Returns the this'th FAT entry, -1 if it is an end-of-file entry. If
110 * new_value is != -1, that FAT entry is replaced by it.
112 int fat_access(struct super_block *sb, int nr, int new_value)
117 if (nr < 2 || MSDOS_SB(sb)->clusters + 2 <= nr) {
118 fat_fs_panic(sb, "invalid access to FAT (entry 0x%08x)", nr);
121 if (new_value == FAT_ENT_EOF)
122 new_value = EOF_FAT(sb);
124 next = __fat_access(sb, nr, new_value);
127 if (next >= BAD_FAT(sb))
133 void fat_cache_init(struct super_block *sb)
135 struct msdos_sb_info *sbi = MSDOS_SB(sb);
138 spin_lock_init(&sbi->cache_lock);
140 for (count = 0; count < FAT_CACHE_NR - 1; count++) {
141 sbi->cache_array[count].start_cluster = 0;
142 sbi->cache_array[count].next = &sbi->cache_array[count + 1];
144 sbi->cache_array[count].start_cluster = 0;
145 sbi->cache_array[count].next = NULL;
146 sbi->cache = sbi->cache_array;
150 fat_cache_lookup(struct inode *inode, int cluster, int *f_clu, int *d_clu)
152 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
153 struct fat_cache *walk;
156 BUG_ON(cluster == 0);
158 first = MSDOS_I(inode)->i_start;
162 spin_lock(&sbi->cache_lock);
164 if (MSDOS_I(inode)->disk_cluster &&
165 MSDOS_I(inode)->file_cluster <= cluster) {
166 *d_clu = MSDOS_I(inode)->disk_cluster;
167 *f_clu = MSDOS_I(inode)->file_cluster;
170 for (walk = sbi->cache; walk; walk = walk->next) {
171 if (walk->start_cluster == first
172 && walk->file_cluster <= cluster
173 && walk->file_cluster > *f_clu) {
174 *d_clu = walk->disk_cluster;
175 *f_clu = walk->file_cluster;
177 printk("cache hit: %d (%d)\n", *f_clu, *d_clu);
179 if (*f_clu == cluster)
184 printk("cache miss\n");
187 spin_unlock(&sbi->cache_lock);
191 static void list_cache(struct super_block *sb)
193 struct msdos_sb_info *sbi = MSDOS_SB(sb);
194 struct fat_cache *walk;
196 for (walk = sbi->cache; walk; walk = walk->next) {
197 if (walk->start_cluster)
198 printk("<%s,%d>(%d,%d) ", sb->s_id,
199 walk->start_cluster, walk->file_cluster,
209 * Cache invalidation occurs rarely, thus the LRU chain is not updated. It
210 * fixes itself after a while.
212 static void __fat_cache_inval_inode(struct inode *inode)
214 struct fat_cache *walk;
215 int first = MSDOS_I(inode)->i_start;
216 MSDOS_I(inode)->file_cluster = MSDOS_I(inode)->disk_cluster = 0;
217 for (walk = MSDOS_SB(inode->i_sb)->cache; walk; walk = walk->next)
218 if (walk->start_cluster == first)
219 walk->start_cluster = 0;
222 void fat_cache_inval_inode(struct inode *inode)
224 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
225 spin_lock(&sbi->cache_lock);
226 __fat_cache_inval_inode(inode);
227 spin_unlock(&sbi->cache_lock);
230 void fat_cache_add(struct inode *inode, int f_clu, int d_clu)
232 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
233 struct fat_cache *walk, *last;
234 int first, prev_f_clu, prev_d_clu;
238 first = MSDOS_I(inode)->i_start;
243 spin_lock(&sbi->cache_lock);
245 if (MSDOS_I(inode)->file_cluster == f_clu)
248 prev_f_clu = MSDOS_I(inode)->file_cluster;
249 prev_d_clu = MSDOS_I(inode)->disk_cluster;
250 MSDOS_I(inode)->file_cluster = f_clu;
251 MSDOS_I(inode)->disk_cluster = d_clu;
258 for (walk = sbi->cache; walk->next; walk = (last = walk)->next) {
259 if (walk->start_cluster == first &&
260 walk->file_cluster == f_clu) {
261 if (walk->disk_cluster != d_clu) {
262 printk(KERN_ERR "FAT: cache corruption "
263 "(i_pos %lld)\n", MSDOS_I(inode)->i_pos);
264 __fat_cache_inval_inode(inode);
271 last->next = walk->next;
272 walk->next = sbi->cache;
280 walk->start_cluster = first;
281 walk->file_cluster = f_clu;
282 walk->disk_cluster = d_clu;
284 walk->next = sbi->cache;
290 spin_unlock(&sbi->cache_lock);
293 int fat_get_cluster(struct inode *inode, int cluster, int *fclus, int *dclus)
295 struct super_block *sb = inode->i_sb;
296 const int limit = sb->s_maxbytes >> MSDOS_SB(sb)->cluster_bits;
299 BUG_ON(MSDOS_I(inode)->i_start == 0);
302 *dclus = MSDOS_I(inode)->i_start;
306 fat_cache_lookup(inode, cluster, fclus, dclus);
307 while (*fclus < cluster) {
308 /* prevent the infinite loop of cluster chain */
309 if (*fclus > limit) {
310 fat_fs_panic(sb, "%s: detected the cluster chain loop"
311 " (i_pos %lld)", __FUNCTION__,
312 MSDOS_I(inode)->i_pos);
316 nr = fat_access(sb, *dclus, -1);
319 else if (nr == FAT_ENT_FREE) {
320 fat_fs_panic(sb, "%s: invalid cluster chain"
321 " (i_pos %lld)", __FUNCTION__,
322 MSDOS_I(inode)->i_pos);
324 } else if (nr == FAT_ENT_EOF) {
325 fat_cache_add(inode, *fclus, *dclus);
331 fat_cache_add(inode, *fclus, *dclus);
335 static int fat_bmap_cluster(struct inode *inode, int cluster)
337 struct super_block *sb = inode->i_sb;
338 int ret, fclus, dclus;
340 if (MSDOS_I(inode)->i_start == 0)
343 ret = fat_get_cluster(inode, cluster, &fclus, &dclus);
346 else if (ret == FAT_ENT_EOF) {
347 fat_fs_panic(sb, "%s: request beyond EOF (i_pos %lld)",
348 __FUNCTION__, MSDOS_I(inode)->i_pos);
354 int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys)
356 struct super_block *sb = inode->i_sb;
357 struct msdos_sb_info *sbi = MSDOS_SB(sb);
362 if ((sbi->fat_bits != 32) &&
363 (inode->i_ino == MSDOS_ROOT_INO || (S_ISDIR(inode->i_mode) &&
364 !MSDOS_I(inode)->i_start))) {
365 if (sector < (sbi->dir_entries >> sbi->dir_per_block_bits))
366 *phys = sector + sbi->dir_start;
369 last_block = (MSDOS_I(inode)->mmu_private + (sb->s_blocksize - 1))
370 >> sb->s_blocksize_bits;
371 if (sector >= last_block)
374 cluster = sector >> (sbi->cluster_bits - sb->s_blocksize_bits);
375 offset = sector & (sbi->sec_per_clus - 1);
376 cluster = fat_bmap_cluster(inode, cluster);
380 *phys = ((sector_t)cluster - 2) * sbi->sec_per_clus
381 + sbi->data_start + offset;
386 /* Free all clusters after the skip'th cluster. */
387 int fat_free(struct inode *inode, int skip)
389 struct super_block *sb = inode->i_sb;
390 int nr, ret, fclus, dclus;
392 if (MSDOS_I(inode)->i_start == 0)
396 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
399 else if (ret == FAT_ENT_EOF)
402 nr = fat_access(sb, dclus, -1);
403 if (nr == FAT_ENT_EOF)
407 * write a new EOF, and get the remaining cluster
410 nr = fat_access(sb, dclus, FAT_ENT_EOF);
415 fat_cache_inval_inode(inode);
417 fat_cache_inval_inode(inode);
419 nr = MSDOS_I(inode)->i_start;
420 MSDOS_I(inode)->i_start = 0;
421 MSDOS_I(inode)->i_logstart = 0;
422 mark_inode_dirty(inode);
427 nr = fat_access(sb, nr, FAT_ENT_FREE);
430 else if (nr == FAT_ENT_FREE) {
431 fat_fs_panic(sb, "%s: deleting beyond EOF (i_pos %lld)",
432 __FUNCTION__, MSDOS_I(inode)->i_pos);
436 if (MSDOS_SB(sb)->free_clusters != -1)
437 MSDOS_SB(sb)->free_clusters++;
438 inode->i_blocks -= MSDOS_SB(sb)->cluster_size >> 9;
439 } while (nr != FAT_ENT_EOF);
440 fat_clusters_flush(sb);