MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / fat / prince-fat / fat-cache-atomic / cache.c
blob2d8d3b4d6bad9096a92cb7e1e658d00cd18f9775
1 /*
2 * linux/fs/fat/cache.c
4 * Written 1992,1993 by Werner Almesberger
6 * Mar 1999. AV. Changed cache, so that it uses the starting cluster instead
7 * of inode number.
8 * May 1999. AV. Fixed the bogosity with FAT32 (read "FAT28"). Fscking lusers.
9 */
11 #include <linux/fs.h>
12 #include <linux/msdos_fs.h>
13 #include <linux/buffer_head.h>
14 //prince debug
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) {
25 first = last = nr*4;
26 } else if (sbi->fat_bits == 16) {
27 first = last = nr*2;
28 } else {
29 first = nr*3/2;
30 last = first+1;
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);
36 return -EIO;
38 if ((first >> sb->s_blocksize_bits) == (last >> sb->s_blocksize_bits)) {
39 bh2 = bh;
40 } else {
41 if (!(bh2 = sb_bread(sb, b + 1))) {
42 brelse(bh);
43 printk(KERN_ERR "FAT: bread(block %d) in"
44 " fat_access failed\n", b + 1);
45 return -EIO;
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. */
53 next &= 0x0fffffff;
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]);
58 } else {
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)];
61 if (nr & 1)
62 next = ((*p_first >> 4) | (*p_last << 4)) & 0xfff;
63 else
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]
69 = CT_LE_L(new_value);
70 } else if (sbi->fat_bits == 16) {
71 ((__le16 *)bh->b_data)[(first & (sb->s_blocksize - 1)) >> 1]
72 = CT_LE_W(new_value);
73 } else {
74 if (nr & 1) {
75 *p_first = (*p_first & 0xf) | (new_value << 4);
76 *p_last = new_value >> 4;
78 else {
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)))
89 break;
90 if (bh != bh2) {
91 if (!(c_bh2 = sb_bread(sb, b+1))) {
92 brelse(c_bh);
93 break;
95 memcpy(c_bh2->b_data, bh2->b_data, sb->s_blocksize);
96 mark_buffer_dirty(c_bh2);
97 brelse(c_bh2);
99 memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize);
100 mark_buffer_dirty(c_bh);
101 brelse(c_bh);
104 brelse(bh);
105 if (bh != bh2)
106 brelse(bh2);
107 return next;
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)
116 int next;
117 next = -EIO;
119 //prince debug
120 //int count;
122 //prince debug
123 //recheck:
126 if (nr < 2 || MSDOS_SB(sb)->clusters + 2 <= nr) {
127 //prince debug
128 //count++;
129 //if ( count <= MAXFATHEADTEST )
130 // goto recheck;
132 fat_fs_panic(sb, "invalid access to FAT (entry 0x%08x)", nr);
133 goto out;
135 if (new_value == FAT_ENT_EOF)
136 new_value = EOF_FAT(sb);
138 next = __fat_access(sb, nr, new_value);
139 if (next < 0)
140 goto out;
141 if (next >= BAD_FAT(sb))
142 next = FAT_ENT_EOF;
143 out:
144 return next;
147 void fat_cache_init(struct super_block *sb)
149 struct msdos_sb_info *sbi = MSDOS_SB(sb);
150 int count;
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;
163 static void
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;
168 int first;
170 BUG_ON(cluster == 0);
172 first = MSDOS_I(inode)->i_start;
173 if (!first)
174 return;
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;
190 #ifdef DEBUG
191 printk("cache hit: %d (%d)\n", *f_clu, *d_clu);
192 #endif
193 if (*f_clu == cluster)
194 goto out;
197 #ifdef DEBUG
198 printk("cache miss\n");
199 #endif
200 out:
201 spin_unlock(&sbi->cache_lock);
204 #ifdef DEBUG
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,
214 walk->disk_cluster);
215 else
216 printk("-- ");
218 printk("\n");
220 #endif
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;
250 if (f_clu == 0)
251 return;
252 first = MSDOS_I(inode)->i_start;
253 if (!first)
254 return;
256 last = NULL;
257 spin_lock(&sbi->cache_lock);
259 if (MSDOS_I(inode)->file_cluster == f_clu)
260 goto out;
261 else {
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;
266 if (prev_f_clu == 0)
267 goto out;
268 f_clu = prev_f_clu;
269 d_clu = prev_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);
279 goto out;
281 if (last == NULL)
282 goto out;
284 /* update LRU */
285 last->next = walk->next;
286 walk->next = sbi->cache;
287 sbi->cache = walk;
288 #ifdef DEBUG
289 list_cache();
290 #endif
291 goto out;
294 walk->start_cluster = first;
295 walk->file_cluster = f_clu;
296 walk->disk_cluster = d_clu;
297 last->next = NULL;
298 walk->next = sbi->cache;
299 sbi->cache = walk;
300 #ifdef DEBUG
301 list_cache();
302 #endif
303 out:
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;
311 int nr;
314 BUG_ON(MSDOS_I(inode)->i_start == 0);
316 *fclus = 0;
317 *dclus = MSDOS_I(inode)->i_start;
318 if (cluster == 0)
319 return 0;
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);
328 return -EIO;
331 nr = fat_access(sb, *dclus, -1);
332 if (nr < 0)
333 return nr;
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);
338 return -EIO;
339 } else if (nr == FAT_ENT_EOF) {
340 fat_cache_add(inode, *fclus, *dclus);
341 return FAT_ENT_EOF;
343 (*fclus)++;
344 *dclus = nr;
346 fat_cache_add(inode, *fclus, *dclus);
347 return 0;
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)
356 return 0;
358 ret = fat_get_cluster(inode, cluster, &fclus, &dclus);
359 if (ret < 0)
360 return ret;
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);
364 return -EIO;
366 return dclus;
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);
373 sector_t last_block;
374 int cluster, offset;
376 *phys = 0;
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;
382 return 0;
384 last_block = (MSDOS_I(inode)->mmu_private + (sb->s_blocksize - 1))
385 >> sb->s_blocksize_bits;
386 if (sector >= last_block)
387 return 0;
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);
392 if (cluster < 0)
393 return cluster;
394 else if (cluster) {
395 *phys = ((sector_t)cluster - 2) * sbi->sec_per_clus
396 + sbi->data_start + offset;
398 return 0;
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;
407 //prince debug
408 //int count;
410 if (MSDOS_I(inode)->i_start == 0)
411 return 0;
413 if (skip) {
414 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
415 if (ret < 0)
416 return ret;
417 else if (ret == FAT_ENT_EOF)
418 return 0;
420 nr = fat_access(sb, dclus, -1);
421 if (nr == FAT_ENT_EOF)
422 return 0;
423 else if (nr > 0) {
425 * write a new EOF, and get the remaining cluster
426 * chain for freeing.
428 nr = fat_access(sb, dclus, FAT_ENT_EOF);
430 if (nr < 0)
431 return nr;
433 fat_cache_inval_inode(inode);
434 } else {
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);
443 lock_fat(sb);
444 do {
445 //prince debug
446 //recheck:
447 nr = fat_access(sb, nr, FAT_ENT_FREE);
448 if (nr < 0)
449 goto error;
450 else if (nr == FAT_ENT_FREE) {
451 //prince debug
452 //count++;
453 //if ( count <= MAXFATHEADTEST )
454 // goto recheck;
456 fat_fs_panic(sb, "%s: deleting beyond EOF (i_pos %lld)",
457 __FUNCTION__, MSDOS_I(inode)->i_pos);
458 nr = -EIO;
459 goto error;
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);
466 nr = 0;
467 error:
468 unlock_fat(sb);
470 return nr;