This source file includes following definitions.
- fat_access
- cache_init
- cache_lookup
- list_cache
- cache_add
- cache_inval_inode
- cache_inval_dev
- get_cluster
- msdos_smap
- fat_free
1
2
3
4
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 static struct fat_cache *fat_cache,cache[FAT_CACHE];
16
17
18
19
20 int fat_access(struct super_block *sb,int nr,int new_value)
21 {
22 struct buffer_head *bh,*bh2,*c_bh,*c_bh2;
23 unsigned char *p_first,*p_last;
24 int first,last,next,copy;
25
26 if ((unsigned) (nr-2) >= MSDOS_SB(sb)->clusters) return 0;
27 if (MSDOS_SB(sb)->fat_bits == 16) first = last = nr*2;
28 else {
29 first = nr*3/2;
30 last = first+1;
31 }
32 if (!(bh = bread(sb->s_dev,MSDOS_SB(sb)->fat_start+(first >>
33 SECTOR_BITS),SECTOR_SIZE))) {
34 printk("bread in fat_access failed\n");
35 return 0;
36 }
37 if ((first >> SECTOR_BITS) == (last >> SECTOR_BITS))
38 bh2 = bh;
39 else {
40 if (!(bh2 = bread(sb->s_dev,MSDOS_SB(sb)->fat_start+(last
41 >> SECTOR_BITS),SECTOR_SIZE))) {
42 brelse(bh);
43 printk("bread in fat_access failed\n");
44 return 0;
45 }
46 }
47 if (MSDOS_SB(sb)->fat_bits == 16) {
48 p_first = p_last = NULL;
49 next = CF_LE_W(((unsigned short *) bh->b_data)[(first &
50 (SECTOR_SIZE-1)) >> 1]);
51 if (next >= 0xfff7) next = -1;
52 }
53 else {
54 p_first = &((unsigned char *) bh->b_data)[first & (SECTOR_SIZE-1)];
55 p_last = &((unsigned char *) bh2->b_data)[(first+1) &
56 (SECTOR_SIZE-1)];
57 if (nr & 1) next = ((*p_first >> 4) | (*p_last << 4)) & 0xfff;
58 else next = (*p_first+(*p_last << 8)) & 0xfff;
59 if (next >= 0xff7) next = -1;
60 }
61 if (new_value != -1) {
62 if (MSDOS_SB(sb)->fat_bits == 16)
63 ((unsigned short *) bh->b_data)[(first & (SECTOR_SIZE-1)) >>
64 1] = CT_LE_W(new_value);
65 else {
66 if (nr & 1) {
67 *p_first = (*p_first & 0xf) | (new_value << 4);
68 *p_last = new_value >> 4;
69 }
70 else {
71 *p_first = new_value & 0xff;
72 *p_last = (*p_last & 0xf0) | (new_value >> 8);
73 }
74 mark_buffer_dirty(bh2, 1);
75 }
76 mark_buffer_dirty(bh, 1);
77 for (copy = 1; copy < MSDOS_SB(sb)->fats; copy++) {
78 if (!(c_bh = bread(sb->s_dev,MSDOS_SB(sb)->
79 fat_start+(first >> SECTOR_BITS)+MSDOS_SB(sb)->
80 fat_length*copy,SECTOR_SIZE))) break;
81 memcpy(c_bh->b_data,bh->b_data,SECTOR_SIZE);
82 mark_buffer_dirty(c_bh, 1);
83 if (bh != bh2) {
84 if (!(c_bh2 = bread(sb->s_dev,
85 MSDOS_SB(sb)->fat_start+(first >>
86 SECTOR_BITS)+MSDOS_SB(sb)->fat_length*copy
87 +1,SECTOR_SIZE))) {
88 brelse(c_bh);
89 break;
90 }
91 memcpy(c_bh2->b_data,bh2->b_data,SECTOR_SIZE);
92 brelse(c_bh2);
93 }
94 brelse(c_bh);
95 }
96 }
97 brelse(bh);
98 if (bh != bh2) brelse(bh2);
99 return next;
100 }
101
102
103 void cache_init(void)
104 {
105 static int initialized = 0;
106 int count;
107
108 if (initialized) return;
109 fat_cache = &cache[0];
110 for (count = 0; count < FAT_CACHE; count++) {
111 cache[count].device = 0;
112 cache[count].next = count == FAT_CACHE-1 ? NULL :
113 &cache[count+1];
114 }
115 initialized = 1;
116 }
117
118
119 void cache_lookup(struct inode *inode,int cluster,int *f_clu,int *d_clu)
120 {
121 struct fat_cache *walk;
122
123 #ifdef DEBUG
124 printk("cache lookup: <%d,%d> %d (%d,%d) -> ",inode->i_dev,inode->i_ino,cluster,
125 *f_clu,*d_clu);
126 #endif
127 for (walk = fat_cache; walk; walk = walk->next)
128 if (inode->i_dev == walk->device && walk->ino == inode->i_ino &&
129 walk->file_cluster <= cluster && walk->file_cluster >
130 *f_clu) {
131 *d_clu = walk->disk_cluster;
132 #ifdef DEBUG
133 printk("cache hit: %d (%d)\n",walk->file_cluster,*d_clu);
134 #endif
135 if ((*f_clu = walk->file_cluster) == cluster) return;
136 }
137 #ifdef DEBUG
138 printk("cache miss\n");
139 #endif
140 }
141
142
143 #ifdef DEBUG
144 static void list_cache(void)
145 {
146 struct fat_cache *walk;
147
148 for (walk = fat_cache; walk; walk = walk->next) {
149 if (walk->device)
150 printk("<%d,%d>(%d,%d) ",walk->device,walk->ino,
151 walk->file_cluster,walk->disk_cluster);
152 else printk("-- ");
153 }
154 printk("\n");
155 }
156 #endif
157
158
159 void cache_add(struct inode *inode,int f_clu,int d_clu)
160 {
161 struct fat_cache *walk,*last;
162
163 #ifdef DEBUG
164 printk("cache add: <%d,%d> %d (%d)\n",inode->i_dev,inode->i_ino,f_clu,d_clu);
165 #endif
166 last = NULL;
167 for (walk = fat_cache; walk->next; walk = (last = walk)->next)
168 if (inode->i_dev == walk->device && walk->ino == inode->i_ino &&
169 walk->file_cluster == f_clu) {
170 if (walk->disk_cluster != d_clu) {
171 printk("FAT cache corruption");
172 cache_inval_inode(inode);
173 return;
174 }
175
176 if (last == NULL) return;
177 last->next = walk->next;
178 walk->next = fat_cache;
179 fat_cache = walk;
180 #ifdef DEBUG
181 list_cache();
182 #endif
183 return;
184 }
185 walk->device = inode->i_dev;
186 walk->ino = inode->i_ino;
187 walk->file_cluster = f_clu;
188 walk->disk_cluster = d_clu;
189 last->next = NULL;
190 walk->next = fat_cache;
191 fat_cache = walk;
192 #ifdef DEBUG
193 list_cache();
194 #endif
195 }
196
197
198
199
200
201 void cache_inval_inode(struct inode *inode)
202 {
203 struct fat_cache *walk;
204
205 for (walk = fat_cache; walk; walk = walk->next)
206 if (walk->device == inode->i_dev && walk->ino == inode->i_ino)
207 walk->device = 0;
208 }
209
210
211 void cache_inval_dev(int device)
212 {
213 struct fat_cache *walk;
214
215 for (walk = fat_cache; walk; walk = walk->next)
216 if (walk->device == device) walk->device = 0;
217 }
218
219
220 int get_cluster(struct inode *inode,int cluster)
221 {
222 int nr,count;
223
224 if (!(nr = MSDOS_I(inode)->i_start)) return 0;
225 if (!cluster) return nr;
226 count = 0;
227 for (cache_lookup(inode,cluster,&count,&nr); count < cluster;
228 count++) {
229 if ((nr = fat_access(inode->i_sb,nr,-1)) == -1) return 0;
230 if (!nr) return 0;
231 }
232 cache_add(inode,cluster,nr);
233 return nr;
234 }
235
236
237 int msdos_smap(struct inode *inode,int sector)
238 {
239 struct msdos_sb_info *sb;
240 int cluster,offset;
241
242 sb = MSDOS_SB(inode->i_sb);
243 if (inode->i_ino == MSDOS_ROOT_INO || (S_ISDIR(inode->i_mode) &&
244 !MSDOS_I(inode)->i_start)) {
245 if (sector >= sb->dir_entries >> MSDOS_DPS_BITS) return 0;
246 return sector+sb->dir_start;
247 }
248 cluster = sector/sb->cluster_size;
249 offset = sector % sb->cluster_size;
250 if (!(cluster = get_cluster(inode,cluster))) return 0;
251 return (cluster-2)*sb->cluster_size+sb->data_start+offset;
252 }
253
254
255
256
257
258 int fat_free(struct inode *inode,int skip)
259 {
260 int nr,last;
261
262 if (!(nr = MSDOS_I(inode)->i_start)) return 0;
263 last = 0;
264 while (skip--) {
265 last = nr;
266 if ((nr = fat_access(inode->i_sb,nr,-1)) == -1) return 0;
267 if (!nr) {
268 printk("fat_free: skipped EOF\n");
269 return -EIO;
270 }
271 }
272 if (last)
273 fat_access(inode->i_sb,last,MSDOS_SB(inode->i_sb)->fat_bits ==
274 12 ? 0xff8 : 0xfff8);
275 else {
276 MSDOS_I(inode)->i_start = 0;
277 inode->i_dirt = 1;
278 }
279 lock_fat(inode->i_sb);
280 while (nr != -1) {
281 if (!(nr = fat_access(inode->i_sb,nr,0))) {
282 fs_panic(inode->i_sb,"fat_free: deleting beyond EOF");
283 break;
284 }
285 if (MSDOS_SB(inode->i_sb)->free_clusters != -1)
286 MSDOS_SB(inode->i_sb)->free_clusters++;
287 inode->i_blocks -= MSDOS_SB(inode->i_sb)->cluster_size;
288 }
289 unlock_fat(inode->i_sb);
290 cache_inval_inode(inode);
291 return 0;
292 }