OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [fs/] [fat/] [cache.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1628 jcastillo
/*
2
 *  linux/fs/fat/cache.c
3
 *
4
 *  Written 1992,1993 by Werner Almesberger
5
 */
6
 
7
#include <linux/msdos_fs.h>
8
#include <linux/kernel.h>
9
#include <linux/errno.h>
10
#include <linux/string.h>
11
#include <linux/stat.h>
12
 
13
#include "msbuffer.h"
14
 
15
#if 0
16
#  define PRINTK(x) printk x
17
#else
18
#  define PRINTK(x)
19
#endif
20
 
21
static struct fat_cache *fat_cache,cache[FAT_CACHE];
22
 
23
/* Returns the this'th FAT entry, -1 if it is an end-of-file entry. If
24
   new_value is != -1, that FAT entry is replaced by it. */
25
 
26
int fat_access(struct super_block *sb,int nr,int new_value)
27
{
28
        struct buffer_head *bh,*bh2,*c_bh,*c_bh2;
29
        unsigned char *p_first,*p_last;
30
        int copy,first,last,next,b;
31
 
32
        if ((unsigned) (nr-2) >= MSDOS_SB(sb)->clusters)
33
                return 0;
34
        if (MSDOS_SB(sb)->fat_bits == 32) {
35
                first = last = nr*4;
36
        } else if (MSDOS_SB(sb)->fat_bits == 16) {
37
                first = last = nr*2;
38
        } else {
39
                first = nr*3/2;
40
                last = first+1;
41
        }
42
        b = MSDOS_SB(sb)->fat_start + (first >> SECTOR_BITS);
43
        if (!(bh = fat_bread(sb, b))) {
44
                printk("bread in fat_access failed\n");
45
                return 0;
46
        }
47
        if ((first >> SECTOR_BITS) == (last >> SECTOR_BITS)) {
48
                bh2 = bh;
49
        } else {
50
                if (!(bh2 = fat_bread(sb, b+1))) {
51
                        fat_brelse(sb, bh);
52
                        printk("2nd bread in fat_access failed\n");
53
                        return 0;
54
                }
55
        }
56
        if (MSDOS_SB(sb)->fat_bits == 32) {
57
                p_first = p_last = NULL; /* GCC needs that stuff */
58
                next = CF_LE_L(((unsigned long *) bh->b_data)[(first &
59
                    (SECTOR_SIZE-1)) >> 2]);
60
                if (next >= 0xffffff7) next = -1;
61
                PRINTK(("fat_bread: 0x%x, nr=0x%x, first=0x%x, next=0x%d\n", b, nr, first, next));
62
 
63
        } else if (MSDOS_SB(sb)->fat_bits == 16) {
64
                p_first = p_last = NULL; /* GCC needs that stuff */
65
                next = CF_LE_W(((unsigned short *) bh->b_data)[(first &
66
                    (SECTOR_SIZE-1)) >> 1]);
67
                if (next >= 0xfff7) next = -1;
68
        } else {
69
                p_first = &((unsigned char *) bh->b_data)[first & (SECTOR_SIZE-1)];
70
                p_last = &((unsigned char *) bh2->b_data)[(first+1) &
71
                    (SECTOR_SIZE-1)];
72
                if (nr & 1) next = ((*p_first >> 4) | (*p_last << 4)) & 0xfff;
73
                else next = (*p_first+(*p_last << 8)) & 0xfff;
74
                if (next >= 0xff7) next = -1;
75
        }
76
        if (new_value != -1) {
77
                if (MSDOS_SB(sb)->fat_bits == 32) {
78
                        ((unsigned long *) bh->b_data)[(first & (SECTOR_SIZE-1)) >>
79
                            2] = CT_LE_L(new_value);
80
                } else if (MSDOS_SB(sb)->fat_bits == 16) {
81
                        ((unsigned short *) bh->b_data)[(first & (SECTOR_SIZE-1)) >>
82
                            1] = CT_LE_W(new_value);
83
                } else {
84
                        if (nr & 1) {
85
                                *p_first = (*p_first & 0xf) | (new_value << 4);
86
                                *p_last = new_value >> 4;
87
                        }
88
                        else {
89
                                *p_first = new_value & 0xff;
90
                                *p_last = (*p_last & 0xf0) | (new_value >> 8);
91
                        }
92
                        fat_mark_buffer_dirty(sb, bh2, 1);
93
                }
94
                fat_mark_buffer_dirty(sb, bh, 1);
95
                for (copy = 1; copy < MSDOS_SB(sb)->fats; copy++) {
96
                        b = MSDOS_SB(sb)->fat_start + (first >> SECTOR_BITS) +
97
                                MSDOS_SB(sb)->fat_length * copy;
98
                        if (!(c_bh = fat_bread(sb, b)))
99
                                break;
100
                        memcpy(c_bh->b_data,bh->b_data,SECTOR_SIZE);
101
                        fat_mark_buffer_dirty(sb, c_bh, 1);
102
                        if (bh != bh2) {
103
                                if (!(c_bh2 = fat_bread(sb, b+1))) {
104
                                        fat_brelse(sb, c_bh);
105
                                        break;
106
                                }
107
                                memcpy(c_bh2->b_data,bh2->b_data,SECTOR_SIZE);
108
                                fat_brelse(sb, c_bh2);
109
                        }
110
                        fat_brelse(sb, c_bh);
111
                }
112
        }
113
        fat_brelse(sb, bh);
114
        if (bh != bh2)
115
                fat_brelse(sb, bh2);
116
        return next;
117
}
118
 
119
 
120
void cache_init(void)
121
{
122
        static int initialized = 0;
123
        int count;
124
 
125
        if (initialized) return;
126
        fat_cache = &cache[0];
127
        for (count = 0; count < FAT_CACHE; count++) {
128
                cache[count].device = 0;
129
                cache[count].next = count == FAT_CACHE-1 ? NULL :
130
                    &cache[count+1];
131
        }
132
        initialized = 1;
133
}
134
 
135
 
136
void cache_lookup(struct inode *inode,int cluster,int *f_clu,int *d_clu)
137
{
138
        struct fat_cache *walk;
139
 
140
#ifdef DEBUG
141
printk("cache lookup: <%s,%d> %d (%d,%d) -> ", kdevname(inode->i_dev),
142
       inode->i_ino, cluster, *f_clu, *d_clu);
143
#endif
144
        for (walk = fat_cache; walk; walk = walk->next)
145
                if (inode->i_dev == walk->device
146
                    && walk->ino == inode->i_ino
147
                    && walk->file_cluster <= cluster
148
                    && walk->file_cluster > *f_clu) {
149
                        *d_clu = walk->disk_cluster;
150
#ifdef DEBUG
151
printk("cache hit: %d (%d)\n",walk->file_cluster,*d_clu);
152
#endif
153
                        if ((*f_clu = walk->file_cluster) == cluster) return;
154
                }
155
#ifdef DEBUG
156
printk("cache miss\n");
157
#endif
158
}
159
 
160
 
161
#ifdef DEBUG
162
static void list_cache(void)
163
{
164
        struct fat_cache *walk;
165
 
166
        for (walk = fat_cache; walk; walk = walk->next) {
167
                if (walk->device)
168
                        printk("<%s,%d>(%d,%d) ", kdevname(walk->device),
169
                               walk->ino, walk->file_cluster, walk->disk_cluster);
170
                else printk("-- ");
171
        }
172
        printk("\n");
173
}
174
#endif
175
 
176
 
177
void cache_add(struct inode *inode,int f_clu,int d_clu)
178
{
179
        struct fat_cache *walk,*last;
180
 
181
#ifdef DEBUG
182
printk("cache add: <%s,%d> %d (%d)\n", kdevname(inode->i_dev),
183
       inode->i_ino, f_clu, d_clu);
184
#endif
185
        last = NULL;
186
        for (walk = fat_cache; walk->next; walk = (last = walk)->next)
187
                if (inode->i_dev == walk->device
188
                    && walk->ino == inode->i_ino
189
                    && walk->file_cluster == f_clu) {
190
                        if (walk->disk_cluster != d_clu) {
191
                                printk("FAT cache corruption");
192
                                fat_cache_inval_inode(inode);
193
                                return;
194
                        }
195
                        /* update LRU */
196
                        if (last == NULL) return;
197
                        last->next = walk->next;
198
                        walk->next = fat_cache;
199
                        fat_cache = walk;
200
#ifdef DEBUG
201
list_cache();
202
#endif
203
                        return;
204
                }
205
        walk->device = inode->i_dev;
206
        walk->ino = inode->i_ino;
207
        walk->file_cluster = f_clu;
208
        walk->disk_cluster = d_clu;
209
        last->next = NULL;
210
        walk->next = fat_cache;
211
        fat_cache = walk;
212
#ifdef DEBUG
213
list_cache();
214
#endif
215
}
216
 
217
 
218
/* Cache invalidation occurs rarely, thus the LRU chain is not updated. It
219
   fixes itself after a while. */
220
 
221
void fat_cache_inval_inode(struct inode *inode)
222
{
223
        struct fat_cache *walk;
224
 
225
        for (walk = fat_cache; walk; walk = walk->next)
226
                if (walk->device == inode->i_dev
227
                    && walk->ino == inode->i_ino)
228
                        walk->device = 0;
229
}
230
 
231
 
232
void fat_cache_inval_dev(kdev_t device)
233
{
234
        struct fat_cache *walk;
235
 
236
        for (walk = fat_cache; walk; walk = walk->next)
237
                if (walk->device == device)
238
                        walk->device = 0;
239
}
240
 
241
 
242
int get_cluster(struct inode *inode,int cluster)
243
{
244
        int nr,count;
245
 
246
        if (!(nr = MSDOS_I(inode)->i_start)) return 0;
247
        if (!cluster) return nr;
248
        count = 0;
249
        for (cache_lookup(inode,cluster,&count,&nr); count < cluster;
250
            count++) {
251
                if ((nr = fat_access(inode->i_sb,nr,-1)) == -1) return 0;
252
                if (!nr) return 0;
253
        }
254
        cache_add(inode,cluster,nr);
255
        return nr;
256
}
257
 
258
int fat_smap(struct inode *inode,int sector)
259
{
260
        struct msdos_sb_info *sb;
261
        int cluster,offset;
262
 
263
        sb = MSDOS_SB(inode->i_sb);
264
        if ((sb->fat_bits != 32) &&
265
            (inode->i_ino == MSDOS_ROOT_INO || (S_ISDIR(inode->i_mode) &&
266
             !MSDOS_I(inode)->i_start))) {
267
                if (sector >= sb->dir_entries >> MSDOS_DPS_BITS)
268
                        return 0;
269
                return sector+sb->dir_start;
270
        }
271
        cluster = sector/sb->cluster_size;
272
        offset = sector % sb->cluster_size;
273
        if (!(cluster = get_cluster(inode,cluster))) return 0;
274
        return (cluster-2)*sb->cluster_size+sb->data_start+offset;
275
}
276
 
277
 
278
/* Free all clusters after the skip'th cluster. Doesn't use the cache,
279
   because this way we get an additional sanity check. */
280
 
281
int fat_free(struct inode *inode,int skip)
282
{
283
        int nr,last;
284
        int fat_bits;
285
 
286
        if (!(nr = MSDOS_I(inode)->i_start)) return 0;
287
        last = 0;
288
        while (skip--) {
289
                last = nr;
290
                if ((nr = fat_access(inode->i_sb,nr,-1)) == -1) return 0;
291
                if (!nr) {
292
                        printk("fat_free: skipped EOF\n");
293
                        return -EIO;
294
                }
295
        }
296
        if (last) {
297
                fat_bits = MSDOS_SB(inode->i_sb)->fat_bits;
298
                fat_access(inode->i_sb,last,fat_bits == 12 ? EOF_FAT12 :
299
                           fat_bits == 16 ? EOF_FAT16 : EOF_FAT32);
300
        } else {
301
                MSDOS_I(inode)->i_start = 0;
302
                MSDOS_I(inode)->i_logstart = 0;
303
                inode->i_dirt = 1;
304
        }
305
        lock_fat(inode->i_sb);
306
        while (nr != -1) {
307
                if (!(nr = fat_access(inode->i_sb,nr,0))) {
308
                        fat_fs_panic(inode->i_sb,"fat_free: deleting beyond EOF");
309
                        break;
310
                }
311
                if (MSDOS_SB(inode->i_sb)->free_clusters != -1) {
312
                        MSDOS_SB(inode->i_sb)->free_clusters++;
313
                        if (MSDOS_SB(inode->i_sb)->fat_bits == 32) {
314
                                fat_clusters_flush(inode->i_sb);
315
                        }
316
                }
317
                inode->i_blocks -= MSDOS_SB(inode->i_sb)->cluster_size;
318
        }
319
        unlock_fat(inode->i_sb);
320
        fat_cache_inval_inode(inode);
321
        return 0;
322
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.