This source file includes following definitions.
- ext2_file_read
- ext2_file_write
- ext2_release_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <asm/segment.h>
19 #include <asm/system.h>
20
21 #include <linux/errno.h>
22 #include <linux/fs.h>
23 #include <linux/ext2_fs.h>
24 #include <linux/fcntl.h>
25 #include <linux/sched.h>
26 #include <linux/stat.h>
27 #include <linux/locks.h>
28
29 #define NBUF 32
30
31 #define MIN(a,b) (((a)<(b))?(a):(b))
32 #define MAX(a,b) (((a)>(b))?(a):(b))
33
34 #include <linux/fs.h>
35 #include <linux/ext2_fs.h>
36
37 static int ext2_file_read (struct inode *, struct file *, char *, int);
38 static int ext2_file_write (struct inode *, struct file *, const char *, int);
39 static void ext2_release_file (struct inode *, struct file *);
40
41
42
43
44
45 static struct file_operations ext2_file_operations = {
46 NULL,
47 ext2_file_read,
48 ext2_file_write,
49 NULL,
50 NULL,
51 ext2_ioctl,
52 generic_mmap,
53 NULL,
54 ext2_release_file,
55 ext2_sync_file,
56 NULL,
57 NULL,
58 NULL
59 };
60
61 struct inode_operations ext2_file_inode_operations = {
62 &ext2_file_operations,
63 NULL,
64 NULL,
65 NULL,
66 NULL,
67 NULL,
68 NULL,
69 NULL,
70 NULL,
71 NULL,
72 NULL,
73 NULL,
74 NULL,
75 NULL,
76 ext2_bmap,
77 ext2_truncate,
78 ext2_permission,
79 NULL
80 };
81
82 static int ext2_file_read (struct inode * inode, struct file * filp,
83 char * buf, int count)
84 {
85 int read, left, chars;
86 int block, blocks, offset;
87 int bhrequest, uptodate;
88 int clusterblocks;
89 struct buffer_head ** bhb, ** bhe;
90 struct buffer_head * bhreq[NBUF];
91 struct buffer_head * buflist[NBUF];
92 struct super_block * sb;
93 unsigned int size;
94 int err;
95
96 if (!inode) {
97 printk ("ext2_file_read: inode = NULL\n");
98 return -EINVAL;
99 }
100 sb = inode->i_sb;
101 if (!S_ISREG(inode->i_mode)) {
102 ext2_warning (sb, "ext2_file_read", "mode = %07o",
103 inode->i_mode);
104 return -EINVAL;
105 }
106 offset = filp->f_pos;
107 size = inode->i_size;
108 if (offset > size)
109 left = 0;
110 else
111 left = size - offset;
112 if (left > count)
113 left = count;
114 if (left <= 0)
115 return 0;
116 read = 0;
117 block = offset >> EXT2_BLOCK_SIZE_BITS(sb);
118 offset &= (sb->s_blocksize - 1);
119 chars = sb->s_blocksize - offset;
120 size = (size + sb->s_blocksize - 1) >> EXT2_BLOCK_SIZE_BITS(sb);
121 blocks = (left + offset + sb->s_blocksize - 1) >> EXT2_BLOCK_SIZE_BITS(sb);
122 bhb = bhe = buflist;
123 if (filp->f_reada) {
124 if (blocks < read_ahead[MAJOR(inode->i_dev)] >> (EXT2_BLOCK_SIZE_BITS(sb) - 9))
125 blocks = read_ahead[MAJOR(inode->i_dev)] >> (EXT2_BLOCK_SIZE_BITS(sb) - 9);
126 if (block + blocks > size)
127 blocks = size - block;
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142 clusterblocks = 0;
143
144 do {
145 bhrequest = 0;
146 uptodate = 1;
147 while (blocks) {
148 --blocks;
149 #if 1
150 if(!clusterblocks) clusterblocks = ext2_getcluster(inode, block);
151 if(clusterblocks) clusterblocks--;
152 #endif
153
154 *bhb = ext2_getblk (inode, block++, 0, &err);
155 if (*bhb && !buffer_uptodate(*bhb)) {
156 uptodate = 0;
157 bhreq[bhrequest++] = *bhb;
158 }
159
160 if (++bhb == &buflist[NBUF])
161 bhb = buflist;
162
163
164
165
166
167 if (uptodate)
168 break;
169
170 if (bhb == bhe)
171 break;
172 }
173
174
175
176
177 if (bhrequest)
178 ll_rw_block (READ, bhrequest, bhreq);
179
180 do {
181
182
183
184 if (*bhe) {
185 wait_on_buffer (*bhe);
186 if (!buffer_uptodate(*bhe)) {
187 brelse(*bhe);
188 if (++bhe == &buflist[NBUF])
189 bhe = buflist;
190 left = 0;
191 break;
192 }
193 }
194 left -= chars;
195 if (left < 0)
196 chars += left;
197 filp->f_pos += chars;
198 read += chars;
199 if (*bhe) {
200 memcpy_tofs (buf, offset + (*bhe)->b_data,
201 chars);
202 brelse (*bhe);
203 buf += chars;
204 } else {
205 while (chars-- > 0)
206 put_user (0, buf++);
207 }
208 offset = 0;
209 chars = sb->s_blocksize;
210 if (++bhe == &buflist[NBUF])
211 bhe = buflist;
212 } while (left > 0 && bhe != bhb && (!*bhe || !buffer_locked(*bhe)));
213 } while (left > 0);
214
215
216
217
218 while (bhe != bhb) {
219 brelse (*bhe);
220 if (++bhe == &buflist[NBUF])
221 bhe = buflist;
222 }
223 if (!read)
224 return -EIO;
225 filp->f_reada = 1;
226 if (!IS_RDONLY(inode)) {
227 inode->i_atime = CURRENT_TIME;
228 inode->i_dirt = 1;
229 }
230 return read;
231 }
232
233 static int ext2_file_write (struct inode * inode, struct file * filp,
234 const char * buf, int count)
235 {
236 const loff_t two_gb = 2147483647;
237 loff_t pos;
238 off_t pos2;
239 long block;
240 int offset;
241 int written, c;
242 struct buffer_head * bh, *bufferlist[NBUF];
243 struct super_block * sb;
244 int err;
245 int i,buffercount,write_error;
246
247 write_error = buffercount = 0;
248 if (!inode) {
249 printk("ext2_file_write: inode = NULL\n");
250 return -EINVAL;
251 }
252 sb = inode->i_sb;
253 if (sb->s_flags & MS_RDONLY)
254
255
256
257 return -ENOSPC;
258
259 if (!S_ISREG(inode->i_mode)) {
260 ext2_warning (sb, "ext2_file_write", "mode = %07o",
261 inode->i_mode);
262 return -EINVAL;
263 }
264 if (filp->f_flags & O_APPEND)
265 pos = inode->i_size;
266 else
267 pos = filp->f_pos;
268 pos2 = (off_t) pos;
269
270
271
272
273
274
275 if (filp->f_flags & O_SYNC)
276 inode->u.ext2_i.i_osync++;
277 block = pos2 >> EXT2_BLOCK_SIZE_BITS(sb);
278 offset = pos2 & (sb->s_blocksize - 1);
279 c = sb->s_blocksize - offset;
280 written = 0;
281 while (count > 0) {
282 if (pos > two_gb) {
283 if (!written)
284 written = -EFBIG;
285 break;
286 }
287 bh = ext2_getblk (inode, block, 1, &err);
288 if (!bh) {
289 if (!written)
290 written = err;
291 break;
292 }
293 count -= c;
294 if (count < 0)
295 c += count;
296 if (c != sb->s_blocksize && !buffer_uptodate(bh)) {
297 ll_rw_block (READ, 1, &bh);
298 wait_on_buffer (bh);
299 if (!buffer_uptodate(bh)) {
300 brelse (bh);
301 if (!written)
302 written = -EIO;
303 break;
304 }
305 }
306 pos2 += c;
307 pos += c;
308 written += c;
309 memcpy_fromfs (bh->b_data + offset, buf, c);
310 buf += c;
311 mark_buffer_uptodate(bh, 1);
312 mark_buffer_dirty(bh, 0);
313 if (filp->f_flags & O_SYNC)
314 bufferlist[buffercount++] = bh;
315 else
316 brelse(bh);
317 if (buffercount == NBUF){
318 ll_rw_block(WRITE, buffercount, bufferlist);
319 for(i=0; i<buffercount; i++){
320 wait_on_buffer(bufferlist[i]);
321 if (!buffer_uptodate(bufferlist[i]))
322 write_error=1;
323 brelse(bufferlist[i]);
324 }
325 buffercount=0;
326 }
327 if(write_error)
328 break;
329 block++;
330 offset = 0;
331 c = sb->s_blocksize;
332 }
333 if ( buffercount ){
334 ll_rw_block(WRITE, buffercount, bufferlist);
335 for(i=0; i<buffercount; i++){
336 wait_on_buffer(bufferlist[i]);
337 if (!buffer_uptodate(bufferlist[i]))
338 write_error=1;
339 brelse(bufferlist[i]);
340 }
341 }
342 if (pos > inode->i_size)
343 inode->i_size = pos;
344 if (filp->f_flags & O_SYNC)
345 inode->u.ext2_i.i_osync--;
346 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
347 filp->f_pos = pos;
348 inode->i_dirt = 1;
349 return written;
350 }
351
352
353
354
355
356
357 static void ext2_release_file (struct inode * inode, struct file * filp)
358 {
359 if (filp->f_mode & 2)
360 ext2_discard_prealloc (inode);
361 }