This source file includes following definitions.
- ext_file_read
- ext_file_write
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #include <asm/segment.h>
16 #include <asm/system.h>
17
18 #include <linux/sched.h>
19 #include <linux/ext_fs.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/fcntl.h>
23 #include <linux/stat.h>
24 #include <linux/locks.h>
25
26 #define NBUF 32
27
28 #define MIN(a,b) (((a)<(b))?(a):(b))
29 #define MAX(a,b) (((a)>(b))?(a):(b))
30
31 #include <linux/fs.h>
32 #include <linux/ext_fs.h>
33
34 static int ext_file_read(struct inode *, struct file *, char *, int);
35 static int ext_file_write(struct inode *, struct file *, const char *, int);
36
37
38
39
40
41 static struct file_operations ext_file_operations = {
42 NULL,
43 ext_file_read,
44 ext_file_write,
45 NULL,
46 NULL,
47 NULL,
48 generic_mmap,
49 NULL,
50 NULL,
51 ext_sync_file
52 };
53
54 struct inode_operations ext_file_inode_operations = {
55 &ext_file_operations,
56 NULL,
57 NULL,
58 NULL,
59 NULL,
60 NULL,
61 NULL,
62 NULL,
63 NULL,
64 NULL,
65 NULL,
66 NULL,
67 generic_readpage,
68 NULL,
69 ext_bmap,
70 ext_truncate,
71 NULL
72 };
73
74 static int ext_file_read(struct inode * inode, struct file * filp, char * buf, int count)
75 {
76 int read,left,chars;
77 int block, blocks, offset;
78 int bhrequest, uptodate;
79 struct buffer_head ** bhb, ** bhe;
80 struct buffer_head * bhreq[NBUF];
81 struct buffer_head * buflist[NBUF];
82 unsigned int size;
83
84 if (!inode) {
85 printk("ext_file_read: inode = NULL\n");
86 return -EINVAL;
87 }
88 if (!S_ISREG(inode->i_mode)) {
89 printk("ext_file_read: mode = %07o\n",inode->i_mode);
90 return -EINVAL;
91 }
92 offset = filp->f_pos;
93 size = inode->i_size;
94 if (offset > size)
95 left = 0;
96 else
97 left = size - offset;
98 if (left > count)
99 left = count;
100 if (left <= 0)
101 return 0;
102 read = 0;
103 block = offset >> BLOCK_SIZE_BITS;
104 offset &= BLOCK_SIZE-1;
105 size = (size + (BLOCK_SIZE-1)) >> BLOCK_SIZE_BITS;
106 blocks = (left + offset + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
107 bhb = bhe = buflist;
108 if (filp->f_reada) {
109 if(blocks < read_ahead[MAJOR(inode->i_dev)] / (BLOCK_SIZE >> 9))
110 blocks = read_ahead[MAJOR(inode->i_dev)] / (BLOCK_SIZE >> 9);
111 if (block + blocks > size)
112 blocks = size - block;
113 }
114
115
116
117
118
119
120
121
122
123
124
125 do {
126 bhrequest = 0;
127 uptodate = 1;
128 while (blocks) {
129 --blocks;
130 *bhb = ext_getblk(inode, block++, 0);
131 if (*bhb && !buffer_uptodate(*bhb)) {
132 uptodate = 0;
133 bhreq[bhrequest++] = *bhb;
134 }
135
136 if (++bhb == &buflist[NBUF])
137 bhb = buflist;
138
139
140
141 if (uptodate)
142 break;
143 if (bhb == bhe)
144 break;
145 }
146
147
148 if (bhrequest)
149 ll_rw_block(READ, bhrequest, bhreq);
150
151 do {
152 if (*bhe) {
153 wait_on_buffer(*bhe);
154 if (!buffer_uptodate(*bhe)) {
155 brelse(*bhe);
156 if (++bhe == &buflist[NBUF])
157 bhe = buflist;
158 left = 0;
159 break;
160 }
161 }
162 if (left < BLOCK_SIZE - offset)
163 chars = left;
164 else
165 chars = BLOCK_SIZE - offset;
166 filp->f_pos += chars;
167 left -= chars;
168 read += chars;
169 if (*bhe) {
170 memcpy_tofs(buf,offset+(*bhe)->b_data,chars);
171 brelse(*bhe);
172 buf += chars;
173 } else {
174 while (chars-->0)
175 put_user(0,buf++);
176 }
177 offset = 0;
178 if (++bhe == &buflist[NBUF])
179 bhe = buflist;
180 } while (left > 0 && bhe != bhb && (!*bhe || !buffer_locked(*bhe)));
181 } while (left > 0);
182
183
184 while (bhe != bhb) {
185 brelse(*bhe);
186 if (++bhe == &buflist[NBUF])
187 bhe = buflist;
188 };
189 if (!read)
190 return -EIO;
191 filp->f_reada = 1;
192 if (!IS_RDONLY(inode)) {
193 inode->i_atime = CURRENT_TIME;
194 inode->i_dirt = 1;
195 }
196 return read;
197 }
198
199 static int ext_file_write(struct inode * inode, struct file * filp, const char * buf, int count)
200 {
201 off_t pos;
202 int written,c;
203 struct buffer_head * bh;
204 char * p;
205
206 if (!inode) {
207 printk("ext_file_write: inode = NULL\n");
208 return -EINVAL;
209 }
210 if (!S_ISREG(inode->i_mode)) {
211 printk("ext_file_write: mode = %07o\n",inode->i_mode);
212 return -EINVAL;
213 }
214
215
216
217
218 if (filp->f_flags & O_APPEND)
219 pos = inode->i_size;
220 else
221 pos = filp->f_pos;
222 written = 0;
223 while (written<count) {
224 bh = ext_getblk(inode,pos/BLOCK_SIZE,1);
225 if (!bh) {
226 if (!written)
227 written = -ENOSPC;
228 break;
229 }
230 c = BLOCK_SIZE - (pos % BLOCK_SIZE);
231 if (c > count-written)
232 c = count-written;
233 if (c != BLOCK_SIZE && !buffer_uptodate(bh)) {
234 ll_rw_block(READ, 1, &bh);
235 wait_on_buffer(bh);
236 if (!buffer_uptodate(bh)) {
237 brelse(bh);
238 if (!written)
239 written = -EIO;
240 break;
241 }
242 }
243 p = (pos % BLOCK_SIZE) + bh->b_data;
244 pos += c;
245 if (pos > inode->i_size) {
246 inode->i_size = pos;
247 inode->i_dirt = 1;
248 }
249 written += c;
250 memcpy_fromfs(p,buf,c);
251 buf += c;
252 mark_buffer_uptodate(bh, 1);
253 mark_buffer_dirty(bh, 0);
254 brelse(bh);
255 }
256 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
257 filp->f_pos = pos;
258 inode->i_dirt = 1;
259 return written;
260 }