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