1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
* linux/fs/msdos/file.c
*
* Written 1992,1993 by Werner Almesberger
*
* MS-DOS regular file handling primitives
*/
#include <asm/segment.h>
#include <asm/system.h>
#include <linux/sched.h>
#include <linux/locks.h>
#include <linux/fs.h>
#include <linux/msdos_fs.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/stat.h>
#include <linux/string.h>
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define PRINTK(x)
#define Printk(x) printk x
static struct file_operations msdos_file_operations = {
NULL, /* lseek - default */
msdos_file_read, /* read */
msdos_file_write, /* write */
NULL, /* readdir - bad */
NULL, /* select - default */
NULL, /* ioctl - default */
generic_mmap, /* mmap */
NULL, /* no special open is needed */
NULL, /* release */
file_fsync /* fsync */
};
struct inode_operations msdos_file_inode_operations = {
&msdos_file_operations, /* default file operations */
NULL, /* create */
NULL, /* lookup */
NULL, /* link */
NULL, /* unlink */
NULL, /* symlink */
NULL, /* mkdir */
NULL, /* rmdir */
NULL, /* mknod */
NULL, /* rename */
NULL, /* readlink */
NULL, /* follow_link */
msdos_bmap, /* bmap */
msdos_truncate, /* truncate */
NULL, /* permission */
NULL /* smap */
};
#define MSDOS_PREFETCH 32
struct msdos_pre {
int file_sector;/* Next sector to read in the prefetch table */
/* This is relative to the file, not the disk */
struct buffer_head *bhlist[MSDOS_PREFETCH]; /* All buffers needed */
int nblist; /* Number of buffers in bhlist */
int nolist; /* index in bhlist */
};
/*
Order the prefetch of more sectors.
*/
static void msdos_prefetch (
struct inode *inode,
struct msdos_pre *pre,
int nb) /* How many must be prefetch at once */
{
struct buffer_head *bhreq[MSDOS_PREFETCH]; /* Buffers not */
/* already read */
int nbreq=0; /* Number of buffers in bhreq */
int i;
for (i=0; i<nb; i++){
int sector = msdos_smap(inode,pre->file_sector);
if (sector != 0){
struct buffer_head *bh;
PRINTK (("fsector2 %d -> %d\n",pre->file_sector-1,sector));
pre->file_sector++;
bh = getblk(inode->i_dev,sector,SECTOR_SIZE);
if (bh == NULL) break;
pre->bhlist[pre->nblist++] = bh;
if (!bh->b_uptodate) bhreq[nbreq++] = bh;
}else{
break;
}
}
if (nbreq > 0) ll_rw_block (READ,nbreq,bhreq);
for (i=pre->nblist; i<MSDOS_PREFETCH; i++) pre->bhlist[i] = NULL;
}
/*
Read a file into user space
*/
int msdos_file_read(
struct inode *inode,
struct file *filp,
char *buf,
int count)
{
char *start = buf;
char *end = buf + count;
int i;
int left_in_file;
struct msdos_pre pre;
if (!inode) {
printk("msdos_file_read: inode = NULL\n");
return -EINVAL;
}
/* S_ISLNK allows for UMSDOS. Should never happen for normal MSDOS */
if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode)) {
printk("msdos_file_read: mode = %07o\n",inode->i_mode);
return -EINVAL;
}
if (filp->f_pos >= inode->i_size || count <= 0) return 0;
/*
Tell the buffer cache which block we expect to read in advance
Since we are limited with the stack, we preread only MSDOS_PREFETCH
because we have to keep the result into the local
arrays pre.bhlist and bhreq.
Each time we process one block in bhlist, we replace
it by a new prefetch block if needed.
*/
PRINTK (("#### ino %ld pos %ld size %ld count %d\n",inode->i_ino,filp->f_pos,inode->i_size,count));
{
/*
We must prefetch complete block, so we must
take in account the offset in the first block.
*/
int count_max = (filp->f_pos & (SECTOR_SIZE-1)) + count;
int to_reada; /* How many block to read all at once */
pre.file_sector = filp->f_pos >> SECTOR_BITS;
to_reada = count_max / SECTOR_SIZE;
if (count_max & (SECTOR_SIZE-1)) to_reada++;
if (filp->f_reada || !MSDOS_I(inode)->i_binary){
/* Doing a read ahead on ascii file make sure we always */
/* pre read enough, since we don't know how many blocks */
/* we really need */
int ahead = read_ahead[MAJOR(inode->i_dev)];
PRINTK (("to_reada %d ahead %d\n",to_reada,ahead));
if (ahead == 0) ahead = 8;
to_reada += ahead;
}
if (to_reada > MSDOS_PREFETCH) to_reada = MSDOS_PREFETCH;
pre.nblist = 0;
msdos_prefetch (inode,&pre,to_reada);
}
pre.nolist = 0;
PRINTK (("count %d ahead %d nblist %d\n",count,read_ahead[MAJOR(inode->i_dev)],pre.nblist));
while ((left_in_file = inode->i_size - filp->f_pos) > 0
&& buf < end){
struct buffer_head *bh = pre.bhlist[pre.nolist];
char *data;
int size,offset;
if (bh == NULL) break;
pre.bhlist[pre.nolist] = NULL;
pre.nolist++;
if (pre.nolist == MSDOS_PREFETCH/2){
memcpy (pre.bhlist,pre.bhlist+MSDOS_PREFETCH/2
,(MSDOS_PREFETCH/2)*sizeof(pre.bhlist[0]));
pre.nblist -= MSDOS_PREFETCH/2;
msdos_prefetch (inode,&pre,MSDOS_PREFETCH/2);
pre.nolist = 0;
}
PRINTK (("file_read pos %ld nblist %d %d %d\n",filp->f_pos,pre.nblist,pre.fetched,count));
wait_on_buffer(bh);
if (!bh->b_uptodate){
/* read error ? */
brelse (bh);
break;
}
offset = filp->f_pos & (SECTOR_SIZE-1);
data = bh->b_data + offset;
size = MIN(SECTOR_SIZE-offset,left_in_file);
if (MSDOS_I(inode)->i_binary) {
size = MIN(size,end-buf);
memcpy_tofs(buf,data,size);
buf += size;
filp->f_pos += size;
}else{
for (; size && buf < end; size--) {
char ch = *data++;
filp->f_pos++;
if (ch == 26){
filp->f_pos = inode->i_size;
break;
}else if (ch != '\r'){
put_fs_byte(ch,buf++);
}
}
}
brelse(bh);
}
PRINTK (("--- %d -> %d\n",count,(int)(buf-start)));
for (i=0; i<pre.nblist; i++) brelse (pre.bhlist[i]);
if (start == buf) return -EIO;
if (!IS_RDONLY(inode)) inode->i_atime = CURRENT_TIME;
filp->f_reada = 1; /* Will be reset if a lseek is done */
return buf-start;
}
/*
Write to a file either from user space
*/
int msdos_file_write(
struct inode *inode,
struct file *filp,
char *buf,
int count)
{
int sector,offset,size,left,written;
int error,carry;
char *start,*to,ch;
struct buffer_head *bh;
int binary_mode = MSDOS_I(inode)->i_binary;
if (!inode) {
printk("msdos_file_write: inode = NULL\n");
return -EINVAL;
}
/* S_ISLNK allows for UMSDOS. Should never happen for normal MSDOS */
if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode)) {
printk("msdos_file_write: mode = %07o\n",inode->i_mode);
return -EINVAL;
}
/*
* ok, append may not work when many processes are writing at the same time
* but so what. That way leads to madness anyway.
*/
if (filp->f_flags & O_APPEND) filp->f_pos = inode->i_size;
if (count <= 0) return 0;
error = carry = 0;
for (start = buf; count || carry; count -= size) {
while (!(sector = msdos_smap(inode,filp->f_pos >> SECTOR_BITS)))
if ((error = msdos_add_cluster(inode)) < 0) break;
if (error) {
msdos_truncate(inode);
break;
}
offset = filp->f_pos & (SECTOR_SIZE-1);
size = MIN(SECTOR_SIZE-offset,MAX(carry,count));
if (binary_mode
&& offset == 0
&& (size == SECTOR_SIZE
|| filp->f_pos + size >= inode->i_size)){
/* No need to read the block first since we will */
/* completely overwrite it */
/* or at least write past the end of file */
if (!(bh = getblk(inode->i_dev,sector,SECTOR_SIZE))){
error = -EIO;
break;
}
}else if (!(bh = msdos_sread(inode->i_dev,sector))) {
error = -EIO;
break;
}
if (binary_mode) {
memcpy_fromfs(bh->b_data+offset,buf,written = size);
buf += size;
}
else {
written = left = SECTOR_SIZE-offset;
to = (char *) bh->b_data+(filp->f_pos & (SECTOR_SIZE-1));
if (carry) {
*to++ = '\n';
left--;
carry = 0;
}
for (size = 0; size < count && left; size++) {
if ((ch = get_fs_byte(buf++)) == '\n') {
*to++ = '\r';
left--;
}
if (!left) carry = 1;
else {
*to++ = ch;
left--;
}
}
written -= left;
}
filp->f_pos += written;
if (filp->f_pos > inode->i_size) {
inode->i_size = filp->f_pos;
inode->i_dirt = 1;
}
bh->b_uptodate = 1;
mark_buffer_dirty(bh, 0);
brelse(bh);
}
if (start == buf)
return error;
inode->i_mtime = inode->i_ctime = CURRENT_TIME;
MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
inode->i_dirt = 1;
return buf-start;
}
void msdos_truncate(struct inode *inode)
{
int cluster;
cluster = SECTOR_SIZE*MSDOS_SB(inode->i_sb)->cluster_size;
(void) fat_free(inode,(inode->i_size+(cluster-1))/cluster);
MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
inode->i_dirt = 1;
}
|